Skip to content

Commit

Permalink
phpcs-filter: Fix Phan issues (#36832)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
anomiex authored Apr 11, 2024
1 parent 65ca9e7 commit d902369
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 34 deletions.
27 changes: 1 addition & 26 deletions projects/packages/phpcs-filter/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion projects/packages/phpcs-filter/.phan/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
return make_phan_config(
dirname( __DIR__ ),
array(
'stubs' => array(),
'stubs' => array(),
'exclude_file_regex' => array(
'tests/fixtures/',
),
)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Fix Phan issues. No change to functionality.


15 changes: 11 additions & 4 deletions projects/packages/phpcs-filter/src/PhpcsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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() );
}
}
}

0 comments on commit d902369

Please sign in to comment.