From d9023691916e007092e40cfd498e9b4b5aa885f9 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 11 Apr 2024 08:05:10 -0400 Subject: [PATCH] phpcs-filter: Fix Phan issues (#36832) * Skip test fixtures. * Use `??` where applicable. * Fix phpdoc types. * Ignore false positives. * Clarify types inline in a few places. * Fix a bug in a test. --- .../packages/phpcs-filter/.phan/baseline.php | 27 +------------------ .../packages/phpcs-filter/.phan/config.php | 5 +++- .../changelog/fix-phan-in-phpcs-filter | 5 ++++ .../packages/phpcs-filter/src/PhpcsFilter.php | 15 ++++++++--- .../tests/php/PhpcsFilterTest.php | 2 +- .../tests/php/StdinBootstrapTest.php | 5 ++-- 6 files changed, 25 insertions(+), 34 deletions(-) create mode 100644 projects/packages/phpcs-filter/changelog/fix-phan-in-phpcs-filter diff --git a/projects/packages/phpcs-filter/.phan/baseline.php b/projects/packages/phpcs-filter/.phan/baseline.php index 089b910bbedfa..3df50068147ad 100644 --- a/projects/packages/phpcs-filter/.phan/baseline.php +++ b/projects/packages/phpcs-filter/.phan/baseline.php @@ -8,34 +8,9 @@ * (can be combined with --load-baseline) */ return [ - // # Issue statistics: - // PhanUndeclaredFunction : 10+ occurrences - // PhanPossiblyUndeclaredVariable : 3 occurrences - // PhanPluginDuplicateConditionalNullCoalescing : 2 occurrences - // PhanTypeMismatchReturnNullable : 2 occurrences - // PhanUndeclaredMethod : 2 occurrences - // PhanUndeclaredTypeParameter : 2 occurrences - // PhanPluginLoopVariableReuse : 1 occurrence - // PhanTypeArraySuspiciousNullable : 1 occurrence - // PhanTypeMismatchReturnProbablyReal : 1 occurrence - + // This baseline has no suppressions // Currently, file_suppressions and directory_suppressions are the only supported suppressions 'file_suppressions' => [ - 'src/PhpcsFilter.php' => ['PhanPossiblyUndeclaredVariable', 'PhanTypeMismatchReturnNullable', 'PhanTypeMismatchReturnProbablyReal', 'PhanUndeclaredMethod', 'PhanUndeclaredTypeParameter'], - 'tests/fixtures/perdir-custom/control/file.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir-custom/file.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir-custom/test/file.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/control/file.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/exclude-3/file.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/exclude-4/file.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/exclude-pattern/excluded1.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/exclude-pattern/excluded2.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/exclude-pattern/excludedfile.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/excludedfile.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/file.php' => ['PhanUndeclaredFunction'], - 'tests/fixtures/perdir/severity-to-0/file.php' => ['PhanUndeclaredFunction'], - 'tests/php/PhpcsFilterTest.php' => ['PhanPluginLoopVariableReuse', 'PhanPossiblyUndeclaredVariable'], - 'tests/php/StdinBootstrapTest.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeArraySuspiciousNullable'], ], // 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed. // (directory_suppressions will currently be ignored by subsequent calls to --save-baseline, but may be preserved in future Phan releases) diff --git a/projects/packages/phpcs-filter/.phan/config.php b/projects/packages/phpcs-filter/.phan/config.php index 49abbf6e48e14..dfde63c51f9f5 100644 --- a/projects/packages/phpcs-filter/.phan/config.php +++ b/projects/packages/phpcs-filter/.phan/config.php @@ -13,6 +13,9 @@ return make_phan_config( dirname( __DIR__ ), array( - 'stubs' => array(), + 'stubs' => array(), + 'exclude_file_regex' => array( + 'tests/fixtures/', + ), ) ); diff --git a/projects/packages/phpcs-filter/changelog/fix-phan-in-phpcs-filter b/projects/packages/phpcs-filter/changelog/fix-phan-in-phpcs-filter new file mode 100644 index 0000000000000..0bb210470483b --- /dev/null +++ b/projects/packages/phpcs-filter/changelog/fix-phan-in-phpcs-filter @@ -0,0 +1,5 @@ +Significance: patch +Type: fixed +Comment: Fix Phan issues. No change to functionality. + + diff --git a/projects/packages/phpcs-filter/src/PhpcsFilter.php b/projects/packages/phpcs-filter/src/PhpcsFilter.php index 51291204abd33..c433b946c23f2 100644 --- a/projects/packages/phpcs-filter/src/PhpcsFilter.php +++ b/projects/packages/phpcs-filter/src/PhpcsFilter.php @@ -15,6 +15,7 @@ use PHP_CodeSniffer\Filters\Filter; use PHP_CodeSniffer\Ruleset; use PHP_CodeSniffer\Util; +use SplFileInfo; /** * A filter for PHP CodeSniffer to add support for .phpcsignore files and per-directory configuration files. @@ -280,6 +281,7 @@ protected function shouldProcessFile( $path ) { } finally { list( $this->config, $this->ruleset ) = $old; } + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable,PhanTypeMismatchReturnNullable -- https://github.com/phan/phan/issues/4419 return $ret; } @@ -302,16 +304,19 @@ protected function shouldIgnorePath( $path ) { } finally { list( $this->config, $this->ruleset ) = $old; } + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable,PhanTypeMismatchReturnNullable -- https://github.com/phan/phan/issues/4419 return $ret; } /** * Map the input string or SplFileInfo into a LocalFile. * - * @return LocalFile + * @return string|LocalFile */ public function current() { - $filePath = (string) $this->getInnerIterator()->current(); + $iterator = $this->getInnerIterator(); + '@phan-var \RecursiveIterator $iterator'; // Phan has the type wrong somehow. This is the iterator passed to `__construct()`. + $filePath = (string) $iterator->current(); if ( is_dir( $filePath ) ) { return $filePath; } @@ -325,8 +330,10 @@ public function current() { * @return static */ public function getChildren() { - $filePath = $this->getInnerIterator()->current(); - list( $config, $ruleset ) = $this->getConfigAndRuleset( (string) $filePath ); + $iterator = $this->getInnerIterator(); + '@phan-var \RecursiveIterator $iterator'; // Phan has the type wrong somehow. This is the iterator passed to `__construct()`. + $filePath = (string) $iterator->current(); + list( $config, $ruleset ) = $this->getConfigAndRuleset( $filePath ); return new static( new \RecursiveDirectoryIterator( $filePath, \RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS ), $this->basedir, diff --git a/projects/packages/phpcs-filter/tests/php/PhpcsFilterTest.php b/projects/packages/phpcs-filter/tests/php/PhpcsFilterTest.php index 9ea4f376197c8..55bc5e72e396d 100644 --- a/projects/packages/phpcs-filter/tests/php/PhpcsFilterTest.php +++ b/projects/packages/phpcs-filter/tests/php/PhpcsFilterTest.php @@ -220,7 +220,7 @@ public function testRun( $path ) { } sort( $data[ $line ] ); } - foreach ( $file->getWarnings() as $line => $msgs ) { + foreach ( $file->getWarnings() as $line => $cols ) { foreach ( $cols as $msgs ) { foreach ( $msgs as $msg ) { $data[ $line ][] = $msg['source']; diff --git a/projects/packages/phpcs-filter/tests/php/StdinBootstrapTest.php b/projects/packages/phpcs-filter/tests/php/StdinBootstrapTest.php index 869051a75c465..bac25c0732ff1 100644 --- a/projects/packages/phpcs-filter/tests/php/StdinBootstrapTest.php +++ b/projects/packages/phpcs-filter/tests/php/StdinBootstrapTest.php @@ -58,9 +58,10 @@ private function runPhpcs( $args, $content ) { $data = json_decode( $ret, true ); $this->assertIsArray( $data, 'phpcs output contains a JSON object' ); + '@phan-var array $data'; $fileData = array_values( $data['files'] ); - $messages = isset( $fileData[0]['messages'] ) ? $fileData[0]['messages'] : array(); + $messages = $fileData[0]['messages'] ?? array(); $actual = array(); foreach ( $messages as $m ) { @@ -109,7 +110,7 @@ public function provideFiles() { continue; } $contents = file_get_contents( $path ); - yield array( $file, $contents, isset( $expect[ $file ] ) ? $expect[ $file ] : array() ); + yield array( $file, $contents, $expect[ $file ] ?? array() ); } } }