Browse Source

DX: Fix SCA findings (#6626)

SpacePossum 2 years ago
parent
commit
daf7015816

+ 2 - 2
phpstan.neon

@@ -12,7 +12,7 @@ parameters:
         - tests/Fixtures
     ignoreErrors:
         - '/^Class [a-zA-Z\\]+ extends @final class PhpCsFixer\\(ConfigurationException\\InvalidConfigurationException|ConfigurationException\\InvalidFixerConfigurationException|Tokenizer\\Tokens)\.$/'
-        - '/^Unsafe call to private method [a-zA-Z\\]+::[a-zA-Z]+\(\) through static::\.$/'
+        - '/^Unsafe call to private method PhpCsFixer\\Tests\\[a-zA-Z\\]+::[a-zA-Z]+\(\) through static::\.$/'
         - '/^\$this\(PhpCsFixer\\Tokenizer\\Tokens\) does not accept PhpCsFixer\\Tokenizer\\Token\|null\.$/'
 
         # ignore PHPUnit data providers return type as they are not checked against the test methods anyway
@@ -22,6 +22,6 @@ parameters:
         -
             message: '#^.+no value type specified in iterable type.+\.$#'
             path: src/Fixer
-            count: 117
+            count: 40
     tipsOfTheDay: false
     tmpDir: dev-tools/phpstan/cache

+ 3 - 3
src/Fixer/Alias/EregToPregFixer.php

@@ -30,8 +30,8 @@ use PhpCsFixer\Tokenizer\Tokens;
 final class EregToPregFixer extends AbstractFixer
 {
     /**
-     * @var array the list of the ext/ereg function names, their preg equivalent and the preg modifier(s), if any
-     *            all condensed in an array of arrays
+     * @var list<array<int, string>> the list of the ext/ereg function names, their preg equivalent and the preg modifier(s), if any
+     *                               all condensed in an array of arrays
      */
     private static array $functions = [
         ['ereg', 'preg_match', ''],
@@ -43,7 +43,7 @@ final class EregToPregFixer extends AbstractFixer
     ];
 
     /**
-     * @var array the list of preg delimiters, in order of preference
+     * @var list<string> the list of preg delimiters, in order of preference
      */
     private static array $delimiters = ['/', '#', '!'];
 

+ 16 - 2
src/Fixer/Alias/MbStrFunctionsFixer.php

@@ -28,7 +28,15 @@ use PhpCsFixer\Tokenizer\Tokens;
 final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
 {
     /**
-     * @var array the list of the string-related function names and their mb_ equivalent
+     * list of the string-related function names and their mb_ equivalent.
+     *
+     * @var array<
+     *     string,
+     *     array{
+     *         alternativeName: string,
+     *         argumentCount: list<int>,
+     *     },
+     * >
      */
     private static array $functionsMap = [
         'str_split' => ['alternativeName' => 'mb_str_split', 'argumentCount' => [1, 2, 3]],
@@ -47,7 +55,13 @@ final class MbStrFunctionsFixer extends AbstractFunctionReferenceFixer
     ];
 
     /**
-     * @var array<string, array>
+     * @var array<
+     *     string,
+     *     array{
+     *         alternativeName: string,
+     *         argumentCount: list<int>,
+     *     },
+     * >
      */
     private array $functions;
 

+ 5 - 0
src/Fixer/Alias/ModernizeStrposFixer.php

@@ -128,6 +128,9 @@ if (strpos($haystack, $needle) === false) {}
         }
     }
 
+    /**
+     * @param array{operator_index: int, operand_index: int} $operatorIndices
+     */
     private function fixCall(Tokens $tokens, int $functionIndex, array $operatorIndices): void
     {
         foreach (self::REPLACEMENTS as $replacement) {
@@ -163,6 +166,8 @@ if (strpos($haystack, $needle) === false) {}
 
     /**
      * @param -1|1 $direction
+     *
+     * @return null|array{operator_index: int, operand_index: int}
      */
     private function getCompareTokens(Tokens $tokens, int $offsetIndex, int $direction): ?array
     {

+ 3 - 0
src/Fixer/Alias/RandomApiMigrationFixer.php

@@ -32,6 +32,9 @@ use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
  */
 final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer implements ConfigurableFixerInterface
 {
+    /**
+     * @var array<string, array<int, int>>
+     */
     private static array $argumentCounts = [
         'getrandmax' => [0],
         'mt_rand' => [1, 2],

+ 5 - 2
src/Fixer/Alias/SetTypeToCastFixer.php

@@ -156,13 +156,16 @@ settype($bar, "null");
             );
 
             if ('null' === $type) {
-                $this->findSettypeNullCall($tokens, $functionNameIndex, $argumentToken);
+                $this->fixSettypeNullCall($tokens, $functionNameIndex, $argumentToken);
             } else {
                 $this->fixSettypeCall($tokens, $functionNameIndex, $argumentToken, new Token($map[$type]));
             }
         }
     }
 
+    /**
+     * @return list<list<int>>
+     */
     private function findSettypeCalls(Tokens $tokens): array
     {
         $candidates = [];
@@ -225,7 +228,7 @@ settype($bar, "null");
         $tokens->removeTrailingWhitespace($functionNameIndex + 6); // 6 = number of inserted tokens -1 for offset correction
     }
 
-    private function findSettypeNullCall(
+    private function fixSettypeNullCall(
         Tokens $tokens,
         int $functionNameIndex,
         Token $argumentToken

+ 3 - 0
src/Fixer/Casing/ClassReferenceNameCasingFixer.php

@@ -153,6 +153,9 @@ final class ClassReferenceNameCasingFixer extends AbstractFixer
         }
     }
 
+    /**
+     * @return array<string, string>
+     */
     private function getClassNames(): array
     {
         static $classes = null;

+ 3 - 0
src/Fixer/ClassNotation/FinalPublicMethodForAbstractClassFixer.php

@@ -26,6 +26,9 @@ use PhpCsFixer\Tokenizer\Tokens;
  */
 final class FinalPublicMethodForAbstractClassFixer extends AbstractFixer
 {
+    /**
+     * @var array<string, true>
+     */
     private array $magicMethods = [
         '__construct' => true,
         '__destruct' => true,

+ 14 - 8
src/Fixer/ClassNotation/NoPhp4ConstructorFixer.php

@@ -352,19 +352,25 @@ class Foo
     /**
      * Find a function or method matching a given name within certain bounds.
      *
+     * Returns:
+     * - nameIndex (int): The index of the function/method name.
+     * - startIndex (int): The index of the function/method start.
+     * - endIndex (int): The index of the function/method end.
+     * - bodyIndex (int): The index of the function/method body.
+     * - modifiers (array): The modifiers as array keys and their index as the values, e.g. array(T_PUBLIC => 10)
+     *
      * @param Tokens $tokens     the Tokens instance
      * @param string $name       the function/Method name
      * @param int    $startIndex the search start index
      * @param int    $endIndex   the search end index
      *
-     * @return null|array An associative array, if a match is found:
-     *
-     *     - nameIndex (int): The index of the function/method name.
-     *     - startIndex (int): The index of the function/method start.
-     *     - endIndex (int): The index of the function/method end.
-     *     - bodyIndex (int): The index of the function/method body.
-     *     - modifiers (array): The modifiers as array keys and their index as
-     *       the values, e.g. array(T_PUBLIC => 10)
+     * @return null|array{
+     *     nameIndex: int,
+     *     startIndex: int,
+     *     endIndex: int,
+     *     bodyIndex: int,
+     *     modifiers: list<int>,
+     * }
      */
     private function findFunction(Tokens $tokens, string $name, int $startIndex, int $endIndex): ?array
     {

+ 61 - 10
src/Fixer/ClassNotation/OrderedClassElementsFixer.php

@@ -44,7 +44,7 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura
     ];
 
     /**
-     * @var array Array containing all class element base types (keys) and their parent types (values)
+     * @var array<string, null|list<string>> Array containing all class element base types (keys) and their parent types (values)
      */
     private static array $typeHierarchy = [
         'use_trait' => null,
@@ -85,7 +85,7 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura
     ];
 
     /**
-     * @var array Array containing special method types
+     * @var array<string, null> Array containing special method types
      */
     private static array $specialTypes = [
         'construct' => null,
@@ -95,9 +95,9 @@ final class OrderedClassElementsFixer extends AbstractFixer implements Configura
     ];
 
     /**
-     * @var array Resolved configuration array (type => position)
+     * @var array<string, int> Resolved configuration array (type => position)
      */
-    private $typePosition;
+    private array $typePosition;
 
     /**
      * {@inheritdoc}
@@ -295,7 +295,16 @@ class Example
     }
 
     /**
-     * @return array[]
+     * @return list<array{
+     *     start: int,
+     *     visibility: string,
+     *     abstract: bool,
+     *     static: bool,
+     *     readonly: bool,
+     *     type: string,
+     *     name: string,
+     *     end: int,
+     * }>
      */
     private function getElements(Tokens $tokens, int $startIndex): array
     {
@@ -373,7 +382,7 @@ class Example
     }
 
     /**
-     * @return array|string type or array of type and name
+     * @return array<string>|string type or array of type and name
      */
     private function detectElementType(Tokens $tokens, int $index)
     {
@@ -441,9 +450,17 @@ class Example
     }
 
     /**
-     * @param array[] $elements
-     *
-     * @return array[]
+     * @return list<array{
+     *     start: int,
+     *     visibility: string,
+     *     abstract: bool,
+     *     static: bool,
+     *     readonly: bool,
+     *     type: string,
+     *     name: string,
+     *     end: int,
+     *     position: int,
+     * }>
      */
     private function sortElements(array $elements): array
     {
@@ -509,6 +526,30 @@ class Example
         return $elements;
     }
 
+    /**
+     * @param array{
+     *     start: int,
+     *     visibility: string,
+     *     abstract: bool,
+     *     static: bool,
+     *     readonly: bool,
+     *     type: string,
+     *     name: string,
+     *     end: int,
+     *     position: int,
+     * } $a
+     * @param array{
+     *     start: int,
+     *     visibility: string,
+     *     abstract: bool,
+     *     static: bool,
+     *     readonly: bool,
+     *     type: string,
+     *     name: string,
+     *     end: int,
+     *     position: int,
+     * } $b
+     */
     private function sortGroupElements(array $a, array $b): int
     {
         $selectedSortAlgorithm = $this->configuration['sort_algorithm'];
@@ -521,7 +562,17 @@ class Example
     }
 
     /**
-     * @param array[] $elements
+     * @param list<array{
+     *     start: int,
+     *     visibility: string,
+     *     abstract: bool,
+     *     static: bool,
+     *     readonly: bool,
+     *     type: string,
+     *     name: string,
+     *     end: int,
+     *     position: int,
+     * }> $elements
      */
     private function sortTokens(Tokens $tokens, int $startIndex, int $endIndex, array $elements): void
     {

Some files were not shown because too many files changed in this diff