Browse Source

chore: fix PHPStan 1.10.60 issues (#7873)

Kuba Werłos 1 year ago
parent
commit
291fdbf856
3 changed files with 51 additions and 72 deletions
  1. 0 65
      dev-tools/phpstan/baseline.php
  2. 50 3
      src/Preg.php
  3. 1 4
      src/Tokenizer/Analyzer/DataProviderAnalyzer.php

+ 0 - 65
dev-tools/phpstan/baseline.php

@@ -6,11 +6,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Console/ConfigurationResolver.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Binary operation "\\+" between int and string results in an error\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/DocBlock/TypeExpression.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Foreach overwrites \\$token with its value variable\\.$#',
 	'count' => 1,
@@ -41,21 +36,11 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/CastNotation/NoShortBoolCastFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Argument of an invalid type string supplied for foreach, only iterables are supported\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Import/FullyQualifiedStrictTypesFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^For loop initial assignment overwrites variable \\$index\\.$#',
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Import/GlobalNamespaceImportFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Parameter \\#1 \\$array of function array_reverse expects array, string given\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Import/GlobalNamespaceImportFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Variable \\$k might not be defined\\.$#',
 	'count' => 1,
@@ -91,26 +76,11 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/PhpUnit/PhpUnitConstructFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Argument of an invalid type string supplied for foreach, only iterables are supported\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixer.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Argument of an invalid type string supplied for foreach, only iterables are supported\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocToCommentFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Foreach overwrites \\$index with its key variable\\.$#',
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocTrimConsecutiveBlankLineSeparationFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Argument of an invalid type string supplied for foreach, only iterables are supported\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Fixer/Phpdoc/PhpdocVarWithoutNameFixer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Variable \\$end might not be defined\\.$#',
 	'count' => 2,
@@ -126,36 +96,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/Fixer/Whitespace/StatementIndentationFixer.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^Method PhpCsFixer\\\\Preg\\:\\:match\\(\\) never assigns null to &\\$matches so it can be removed from the by\\-ref type\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Preg.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Method PhpCsFixer\\\\Preg\\:\\:matchAll\\(\\) never assigns null to &\\$matches so it can be removed from the by\\-ref type\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Preg.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Method PhpCsFixer\\\\Preg\\:\\:replace\\(\\) never assigns null to &\\$count so it can be removed from the by\\-ref type\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Preg.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Method PhpCsFixer\\\\Preg\\:\\:replaceCallback\\(\\) never assigns null to &\\$count so it can be removed from the by\\-ref type\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Preg.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Parameter &\\$matches by\\-ref type of method PhpCsFixer\\\\Preg\\:\\:match\\(\\) expects array\\<string\\>\\|null, \\(int is int \\? array\\<array\\<int, int\\<\\-1, max\\>\\|string\\>\\> \\: \\(int is int \\? array\\<string\\|null\\> \\: \\(int is int \\? array\\<array\\<int, int\\|string\\|null\\>\\> \\: array\\<string\\>\\)\\)\\) given\\.$#',
-	'count' => 2,
-	'path' => __DIR__ . '/../../src/Preg.php',
-];
-$ignoreErrors[] = [
-	'message' => '#^Parameter &\\$matches by\\-ref type of method PhpCsFixer\\\\Preg\\:\\:matchAll\\(\\) expects array\\<string\\>\\|null, \\(int is int \\? array\\<array\\<int, string\\>\\> \\: \\(int is int \\? array\\<int, array\\<string\\>\\> \\: \\(int is int \\? array\\<array\\<int, array\\<int, int\\|string\\>\\>\\> \\: \\(int is int \\? array\\<int, array\\<array\\<int, int\\|string\\>\\>\\> \\: \\(int is int \\? array\\<array\\<int, string\\|null\\>\\> \\: \\(int is int \\? array\\<int, array\\<string\\|null\\>\\> \\: \\(int is int \\? array\\<int, array\\<array\\<int, int\\|string\\|null\\>\\>\\> \\: array\\)\\)\\)\\)\\)\\)\\) given\\.$#',
-	'count' => 2,
-	'path' => __DIR__ . '/../../src/Preg.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Parameter \\#1 \\$className \\(string\\) of method PhpCsFixer\\\\StdinFileInfo\\:\\:getFileInfo\\(\\) should be contravariant with parameter \\$class \\(string\\|null\\) of method SplFileInfo\\:\\:getFileInfo\\(\\)$#',
 	'count' => 1,
@@ -166,11 +106,6 @@ $ignoreErrors[] = [
 	'count' => 1,
 	'path' => __DIR__ . '/../../src/StdinFileInfo.php',
 ];
-$ignoreErrors[] = [
-	'message' => '#^PHPDoc tag @var with type array\\<int, string\\> is not subtype of type string\\.$#',
-	'count' => 1,
-	'path' => __DIR__ . '/../../src/Tokenizer/Analyzer/DataProviderAnalyzer.php',
-];
 $ignoreErrors[] = [
 	'message' => '#^Parameter \\#1 \\$array \\(array\\<int, PhpCsFixer\\\\Tokenizer\\\\Token\\>\\) of method PhpCsFixer\\\\Tokenizer\\\\Tokens\\:\\:fromArray\\(\\) should be contravariant with parameter \\$array \\(array\\<int, mixed\\>\\) of method SplFixedArray\\<PhpCsFixer\\\\Tokenizer\\\\Token\\>\\:\\:fromArray\\(\\)$#',
 	'count' => 1,

+ 50 - 3
src/Preg.php

@@ -25,8 +25,23 @@ namespace PhpCsFixer;
 final class Preg
 {
     /**
-     * @param null|string[]         $matches
-     * @param int-mask<0, 256, 512> $flags
+     * @todo replace template with variable when https://github.com/phpstan/phpstan/issues/10699 is done
+     *
+     * @template TFlags as int-mask<PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>
+     *
+     * @param array<array-key, mixed> $matches
+     * @param TFlags                  $flags
+     *
+     * @param-out (TFlags is PREG_OFFSET_CAPTURE
+     *     ? array<array-key, array{string, 0|positive-int}|array{'', -1}>
+     *     : (TFlags is PREG_UNMATCHED_AS_NULL
+     *         ? array<array-key, string|null>
+     *         : (TFlags is int-mask<PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>&768
+     *             ? array<array-key, array{string, 0|positive-int}|array{null, -1}>
+     *             : array<array-key, string>
+     *         )
+     *     )
+     * ) $matches
      *
      * @throws PregException
      */
@@ -46,7 +61,35 @@ final class Preg
     }
 
     /**
-     * @param null|string[] $matches
+     * @todo replace template with variable when https://github.com/phpstan/phpstan/issues/10699 is done
+     *
+     * @template TFlags as int-mask<PREG_PATTERN_ORDER, PREG_SET_ORDER, PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>
+     *
+     * @param array<array-key, mixed> $matches
+     * @param TFlags                  $flags
+     *
+     * @param-out (TFlags is PREG_PATTERN_ORDER
+     *     ? array<list<string>>
+     *     : (TFlags is PREG_SET_ORDER
+     *         ? list<array<string>>
+     *         : (TFlags is int-mask<PREG_PATTERN_ORDER, PREG_OFFSET_CAPTURE>&(256|257)
+     *             ? array<list<array{string, int}>>
+     *             : (TFlags is int-mask<PREG_SET_ORDER, PREG_OFFSET_CAPTURE>&258
+     *                 ? list<array<array{string, int}>>
+     *                 : (TFlags is int-mask<PREG_PATTERN_ORDER, PREG_UNMATCHED_AS_NULL>&(512|513)
+     *                     ? array<list<?string>>
+     *                     : (TFlags is int-mask<PREG_SET_ORDER, PREG_UNMATCHED_AS_NULL>&514
+     *                         ? list<array<?string>>
+     *                         : (TFlags is int-mask<PREG_SET_ORDER, PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL>&770
+     *                             ? list<array<array{?string, int}>>
+     *                             : array
+     *                         )
+     *                     )
+     *                 )
+     *             )
+     *         )
+     *     )
+     * ) $matches
      *
      * @throws PregException
      */
@@ -68,6 +111,8 @@ final class Preg
     /**
      * @param string|string[] $subject
      *
+     * @param-out int $count
+     *
      * @throws PregException
      */
     public static function replace(string $pattern, string $replacement, $subject, int $limit = -1, ?int &$count = null): string
@@ -86,6 +131,8 @@ final class Preg
     }
 
     /**
+     * @param-out int $count
+     *
      * @throws PregException
      */
     public static function replaceCallback(string $pattern, callable $callback, string $subject, int $limit = -1, ?int &$count = null): string

+ 1 - 4
src/Tokenizer/Analyzer/DataProviderAnalyzer.php

@@ -50,10 +50,7 @@ final class DataProviderAnalyzer
 
             Preg::matchAll('/@dataProvider\h+(('.self::REGEX_CLASS.'::)?'.TypeExpression::REGEX_IDENTIFIER.')/', $tokens[$docCommentIndex]->getContent(), $matches);
 
-            /** @var list<string> $matches */
-            $matches = $matches[1];
-
-            foreach ($matches as $dataProviderName) {
+            foreach ($matches[1] as $dataProviderName) {
                 $dataProviders[$dataProviderName][] = $docCommentIndex;
             }
         }