Browse Source

Apply PHP 7.1 typing

Dariusz Ruminski 4 years ago
parent
commit
b567e30afc

+ 2 - 1
.github/workflows/ci.yml

@@ -12,6 +12,7 @@ jobs:
         include:
           - operating-system: 'ubuntu-20.04'
             php-version: '7.1'
+            job-description: 'with lowest deps'
             composer-flags: '--prefer-stable --prefer-lowest' # should be checked on lowest supported PHP version
 
           - operating-system: 'ubuntu-20.04'
@@ -114,7 +115,7 @@ jobs:
 
       - name: Execute migration rules
         if: matrix.execute-migration-rules == 'yes'
-        run: php php-cs-fixer fix --rules @PHP80Migration,@PHP80Migration:risky,blank_line_after_opening_tag,-list_syntax -q
+        run: php php-cs-fixer fix --rules @PHP80Migration,@PHP80Migration:risky -q
 
       - name: Disable time limit for tests when collecting coverage
         if: matrix.calculate-code-coverage == 'yes'

+ 2 - 3
.php_cs.dist

@@ -15,7 +15,7 @@ $finder = PhpCsFixer\Finder::create()
     ->in(__DIR__)
     ->append([
         __DIR__.'/dev-tools/doc.php',
-        __DIR__.'/php-cs-fixer',
+        // __DIR__.'/php-cs-fixer', disabled, as we want to be able to run bootstrap file even on lower PHP version, to show nice message
     ])
 ;
 
@@ -23,12 +23,11 @@ $config = new PhpCsFixer\Config();
 $config
     ->setRiskyAllowed(true)
     ->setRules([
-        '@PHP56Migration:risky' => true,
+        '@PHP71Migration:risky' => true,
         '@PHPUnit75Migration:risky' => true,
         '@PhpCsFixer' => true,
         '@PhpCsFixer:risky' => true,
         'header_comment' => ['header' => $header],
-        'list_syntax' => ['syntax' => 'long'],
     ])
     ->setFinder($finder)
 ;

+ 1 - 6
dev-tools/composer-alias-update.php

@@ -5,12 +5,7 @@ require_once __DIR__.'/../vendor/autoload.php';
 
 const COMPOSER_JSON_FILE = __DIR__.'/../composer.json';
 
-/**
- * @param string $version
- *
- * @return string
- */
-function convertAppVersionToAliasedVersion($version)
+function convertAppVersionToAliasedVersion(string $version): string
 {
     $parts = explode('.', $version, 3);
 

+ 0 - 16
phpstan.neon

@@ -16,26 +16,10 @@ parameters:
         -
             message: '/^Strict comparison using !== between ''@git-commit@'' and ''@git-commit@'' will always evaluate to false\.$/'
             path: src/Console/Application.php
-        -
-            message: '/^Result of && is always false\.$/'
-            path: src/Config.php
-        -
-            message: '/^Strict comparison using === between false and true will always evaluate to false\.$/'
-            path: src/Config.php
         -
             message: '/^Else branch is unreachable because ternary operator condition is always true\.$/'
             paths:
-                - src/Config.php
                 - src/Tokenizer/Token.php
-        -
-            message: '/^Parameter #1 \$fixers of method PhpCsFixer\\Config::registerCustomFixers\(\) expects iterable<PhpCsFixer\\Fixer\\FixerInterface>, string given\.$/'
-            path: tests/ConfigTest.php
-        -
-            message: '/^Parameter #1 \$function of function register_shutdown_function expects callable\(\): void, array\(\$this\(PhpCsFixer\\FileRemoval\), ''clean''\) given\.$/'
-            path: src/FileRemoval.php
-        -
-            message: '/^Parameter #1 \$finder of method PhpCsFixer\\Config::setFinder\(\) expects iterable<string>, int given\.$/'
-            path: tests/ConfigTest.php
         - # https://github.com/phpstan/phpstan/issues/1215
             message: '/^Strict comparison using === between false and string will always evaluate to false\.$/'
             path: src/Fixer/StringNotation/NoTrailingWhitespaceInStringFixer.php

+ 9 - 11
src/AbstractDoctrineAnnotationFixer.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of PHP CS Fixer.
  *
@@ -15,6 +17,7 @@ namespace PhpCsFixer;
 use PhpCsFixer\Doctrine\Annotation\Tokens as DoctrineAnnotationTokens;
 use PhpCsFixer\Fixer\ConfigurableFixerInterface;
 use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
+use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
 use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
 use PhpCsFixer\Tokenizer\CT;
 use PhpCsFixer\Tokenizer\Token;
@@ -34,7 +37,7 @@ abstract class AbstractDoctrineAnnotationFixer extends AbstractFixer implements
     /**
      * {@inheritdoc}
      */
-    public function isCandidate(Tokens $tokens)
+    public function isCandidate(Tokens $tokens): bool
     {
         return $tokens->isTokenKindFound(T_DOC_COMMENT);
     }
@@ -42,7 +45,7 @@ abstract class AbstractDoctrineAnnotationFixer extends AbstractFixer implements
     /**
      * {@inheritdoc}
      */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
         // fetch indexes one time, this is safe as we never add or remove a token during fixing
         $analyzer = new TokensAnalyzer($tokens);
@@ -66,17 +69,17 @@ abstract class AbstractDoctrineAnnotationFixer extends AbstractFixer implements
     /**
      * Fixes Doctrine annotations from the given PHPDoc style comment.
      */
-    abstract protected function fixAnnotations(DoctrineAnnotationTokens $doctrineAnnotationTokens);
+    abstract protected function fixAnnotations(DoctrineAnnotationTokens $doctrineAnnotationTokens): void;
 
     /**
      * {@inheritdoc}
      */
-    protected function createConfigurationDefinition()
+    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
     {
         return new FixerConfigurationResolver([
             (new FixerOptionBuilder('ignored_tags', 'List of tags that must not be treated as Doctrine Annotations.'))
                 ->setAllowedTypes(['array'])
-                ->setAllowedValues([static function ($values) {
+                ->setAllowedValues([static function (array $values) {
                     foreach ($values as $value) {
                         if (!\is_string($value)) {
                             return false;
@@ -194,12 +197,7 @@ abstract class AbstractDoctrineAnnotationFixer extends AbstractFixer implements
         ]);
     }
 
-    /**
-     * @param int $index
-     *
-     * @return bool
-     */
-    private function nextElementAcceptsDoctrineAnnotations(Tokens $tokens, $index)
+    private function nextElementAcceptsDoctrineAnnotations(Tokens $tokens, int $index): bool
     {
         do {
             $index = $tokens->getNextMeaningfulToken($index);

+ 13 - 14
src/AbstractFixer.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of PHP CS Fixer.
  *
@@ -63,7 +65,7 @@ abstract class AbstractFixer implements FixerInterface
         }
     }
 
-    final public function fix(\SplFileInfo $file, Tokens $tokens)
+    final public function fix(\SplFileInfo $file, Tokens $tokens): void
     {
         if ($this instanceof ConfigurableFixerInterface && null === $this->configuration) {
             throw new RequiredFixerConfigurationException($this->getName(), 'Configuration is required.');
@@ -77,7 +79,7 @@ abstract class AbstractFixer implements FixerInterface
     /**
      * {@inheritdoc}
      */
-    public function isRisky()
+    public function isRisky(): bool
     {
         return false;
     }
@@ -85,7 +87,7 @@ abstract class AbstractFixer implements FixerInterface
     /**
      * {@inheritdoc}
      */
-    public function getName()
+    public function getName(): string
     {
         $nameParts = explode('\\', static::class);
         $name = substr(end($nameParts), 0, -\strlen('Fixer'));
@@ -96,7 +98,7 @@ abstract class AbstractFixer implements FixerInterface
     /**
      * {@inheritdoc}
      */
-    public function getPriority()
+    public function getPriority(): int
     {
         return 0;
     }
@@ -104,12 +106,12 @@ abstract class AbstractFixer implements FixerInterface
     /**
      * {@inheritdoc}
      */
-    public function supports(\SplFileInfo $file)
+    public function supports(\SplFileInfo $file): bool
     {
         return true;
     }
 
-    public function configure(array $configuration)
+    public function configure(array $configuration): void
     {
         if (!$this instanceof ConfigurableFixerInterface) {
             throw new \LogicException('Cannot configure using Abstract parent, child not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".');
@@ -164,7 +166,7 @@ abstract class AbstractFixer implements FixerInterface
     /**
      * {@inheritdoc}
      */
-    public function getConfigurationDefinition()
+    public function getConfigurationDefinition(): FixerConfigurationResolverInterface
     {
         if (!$this instanceof ConfigurableFixerInterface) {
             throw new \LogicException(sprintf('Cannot get configuration definition using Abstract parent, child "%s" not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".', static::class));
@@ -177,7 +179,7 @@ abstract class AbstractFixer implements FixerInterface
         return $this->configurationDefinition;
     }
 
-    public function setWhitespacesConfig(WhitespacesFixerConfig $config)
+    public function setWhitespacesConfig(WhitespacesFixerConfig $config): void
     {
         if (!$this instanceof WhitespacesAwareFixerInterface) {
             throw new \LogicException('Cannot run method for class not implementing "PhpCsFixer\Fixer\WhitespacesAwareFixerInterface".');
@@ -186,12 +188,9 @@ abstract class AbstractFixer implements FixerInterface
         $this->whitespacesConfig = $config;
     }
 
-    abstract protected function applyFix(\SplFileInfo $file, Tokens $tokens);
+    abstract protected function applyFix(\SplFileInfo $file, Tokens $tokens): void;
 
-    /**
-     * @return FixerConfigurationResolverInterface
-     */
-    protected function createConfigurationDefinition()
+    protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
     {
         if (!$this instanceof ConfigurableFixerInterface) {
             throw new \LogicException('Cannot create configuration definition using Abstract parent, child not implementing "PhpCsFixer\Fixer\ConfigurableFixerInterface".');
@@ -200,7 +199,7 @@ abstract class AbstractFixer implements FixerInterface
         throw new \LogicException('Not implemented.');
     }
 
-    private function getDefaultWhitespacesFixerConfig()
+    private function getDefaultWhitespacesFixerConfig(): WhitespacesFixerConfig
     {
         static $defaultWhitespacesFixerConfig = null;
 

+ 6 - 9
src/AbstractFopenFlagFixer.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of PHP CS Fixer.
  *
@@ -25,7 +27,7 @@ abstract class AbstractFopenFlagFixer extends AbstractFunctionReferenceFixer
     /**
      * {@inheritdoc}
      */
-    public function isCandidate(Tokens $tokens)
+    public function isCandidate(Tokens $tokens): bool
     {
         return $tokens->isAllTokenKindsFound([T_STRING, T_CONSTANT_ENCAPSED_STRING]);
     }
@@ -33,7 +35,7 @@ abstract class AbstractFopenFlagFixer extends AbstractFunctionReferenceFixer
     /**
      * {@inheritdoc}
      */
-    protected function applyFix(\SplFileInfo $file, Tokens $tokens)
+    protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
         $argumentsAnalyzer = new ArgumentsAnalyzer();
 
@@ -71,14 +73,9 @@ abstract class AbstractFopenFlagFixer extends AbstractFunctionReferenceFixer
         }
     }
 
-    abstract protected function fixFopenFlagToken(Tokens $tokens, $argumentStartIndex, $argumentEndIndex);
+    abstract protected function fixFopenFlagToken(Tokens $tokens, int $argumentStartIndex, int $argumentEndIndex): void;
 
-    /**
-     * @param string $mode
-     *
-     * @return bool
-     */
-    protected function isValidModeString($mode)
+    protected function isValidModeString(string $mode): bool
     {
         $modeLength = \strlen($mode);
         if ($modeLength < 1 || $modeLength > 13) { // 13 === length 'r+w+a+x+c+etb'

+ 4 - 6
src/AbstractFunctionReferenceFixer.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of PHP CS Fixer.
  *
@@ -25,7 +27,7 @@ abstract class AbstractFunctionReferenceFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function isRisky()
+    public function isRisky(): bool
     {
         return true;
     }
@@ -34,13 +36,9 @@ abstract class AbstractFunctionReferenceFixer extends AbstractFixer
      * Looks up Tokens sequence for suitable candidates and delivers boundaries information,
      * which can be supplied by other methods in this abstract class.
      *
-     * @param string   $functionNameToSearch
-     * @param int      $start
-     * @param null|int $end
-     *
      * @return null|int[] returns $functionName, $openParenthesis, $closeParenthesis packed into array
      */
-    protected function find($functionNameToSearch, Tokens $tokens, $start = 0, $end = null)
+    protected function find(string $functionNameToSearch, Tokens $tokens, int $start = 0, ?int $end = null): ?array
     {
         // make interface consistent with findSequence
         $end = null === $end ? $tokens->count() : $end;

+ 3 - 2
src/AbstractLinesBeforeNamespaceFixer.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of PHP CS Fixer.
  *
@@ -29,11 +31,10 @@ abstract class AbstractLinesBeforeNamespaceFixer extends AbstractFixer implement
     /**
      * Make sure # of line breaks prefixing namespace is within given range.
      *
-     * @param int $index
      * @param int $expectedMin min. # of line breaks
      * @param int $expectedMax max. # of line breaks
      */
-    protected function fixLinesBeforeNamespace(Tokens $tokens, $index, $expectedMin, $expectedMax)
+    protected function fixLinesBeforeNamespace(Tokens $tokens, int $index, int $expectedMin, int $expectedMax): void
     {
         // Let's determine the total numbers of new lines before the namespace
         // and the opening token

+ 8 - 16
src/AbstractNoUselessElseFixer.php

@@ -1,5 +1,7 @@
 <?php
 
+declare(strict_types=1);
+
 /*
  * This file is part of PHP CS Fixer.
  *
@@ -22,18 +24,13 @@ abstract class AbstractNoUselessElseFixer extends AbstractFixer
     /**
      * {@inheritdoc}
      */
-    public function getPriority()
+    public function getPriority(): int
     {
         // should be run before NoWhitespaceInBlankLineFixer, NoExtraBlankLinesFixer, BracesFixer and after NoEmptyStatementFixer.
         return 25;
     }
 
-    /**
-     * @param int $index
-     *
-     * @return bool
-     */
-    protected function isSuperfluousElse(Tokens $tokens, $index)
+    protected function isSuperfluousElse(Tokens $tokens, int $index): bool
     {
         $previousBlockStart = $index;
 
@@ -104,7 +101,7 @@ abstract class AbstractNoUselessElseFixer extends AbstractFixer
      *
      * @return int[]
      */
-    private function getPreviousBlock(Tokens $tokens, $index)
+    private function getPreviousBlock(Tokens $tokens, int $index): array
     {
         $close = $previous = $tokens->getPrevMeaningfulToken($index);
         // short 'if' detection
@@ -126,10 +123,8 @@ abstract class AbstractNoUselessElseFixer extends AbstractFixer
     /**
      * @param int $index           Index of the token to check
      * @param int $lowerLimitIndex Lower limit index. Since the token to check will always be in a conditional we must stop checking at this index
-     *
-     * @return bool
      */
-    private function isInConditional(Tokens $tokens, $index, $lowerLimitIndex)
+    private function isInConditional(Tokens $tokens, int $index, int $lowerLimitIndex): bool
     {
         $candidateIndex = $tokens->getPrevTokenOfKind($index, [')', ';', ':']);
         if ($tokens[$candidateIndex]->equals(':')) {
@@ -155,12 +150,9 @@ abstract class AbstractNoUselessElseFixer extends AbstractFixer
      * without {}. Assumes not passing the last `;`/close tag of the statement, not
      * out of range index, etc.
      *
-     * @param int $index           Index of the token to check
-     * @param int $lowerLimitIndex
-     *
-     * @return bool
+     * @param int $index Index of the token to check
      */
-    private function isInConditionWithoutBraces(Tokens $tokens, $index, $lowerLimitIndex)
+    private function isInConditionWithoutBraces(Tokens $tokens, int $index, int $lowerLimitIndex): bool
     {
         do {
             if ($tokens[$index]->isComment() || $tokens[$index]->isWhitespace()) {

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