Browse Source

Merge branch 'master' into 3.0

# Conflicts:
#	src/AbstractFixer.php
#	src/Config.php
#	src/Console/ConfigurationResolver.php
#	src/Fixer/ClassNotation/ClassAttributesSeparationFixer.php
#	src/Fixer/FunctionNotation/MethodArgumentSpaceFixer.php
#	src/Fixer/Operator/AlignDoubleArrowFixerHelper.php
#	src/Fixer/Operator/AlignEqualsFixerHelper.php
#	src/Fixer/Operator/BinaryOperatorSpacesFixer.php
#	src/Fixer/Whitespace/BlankLineBeforeStatementFixer.php
#	src/Fixer/Whitespace/NoExtraBlankLinesFixer.php
#	src/FixerConfiguration/FixerConfigurationResolverRootless.php
#	src/FixerDefinition/FixerDefinition.php
#	src/RuleSet/RuleSet.php
#	src/Test/AbstractFixerTestCase.php
#	src/Test/AbstractIntegrationTestCase.php
#	src/Test/AccessibleObject.php
#	src/Test/IntegrationCase.php
#	src/Tokenizer/Token.php
#	src/Tokenizer/Tokens.php
#	tests/UtilsTest.php
Dariusz Ruminski 3 years ago
parent
commit
44aac77a33

+ 9 - 12
src/AbstractFixer.php

@@ -124,19 +124,16 @@ abstract class AbstractFixer implements FixerInterface
 
             $name = $option->getName();
             if (\array_key_exists($name, $configuration)) {
-                $message = sprintf(
-                    'Option "%s" for rule "%s" is deprecated and will be removed in version %d.0. %s',
-                    $name,
-                    $this->getName(),
-                    Application::getMajorVersion() + 1,
-                    str_replace('`', '"', $option->getDeprecationMessage())
+                Utils::triggerDeprecation(
+                    sprintf(
+                        'Option "%s" for rule "%s" is deprecated and will be removed in version %d.0. %s',
+                        $name,
+                        $this->getName(),
+                        Application::getMajorVersion() + 1,
+                        str_replace('`', '"', $option->getDeprecationMessage())
+                    ),
+                    \InvalidArgumentException::class
                 );
-
-                if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
-                    throw new \InvalidArgumentException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
-                }
-
-                @trigger_error($message, E_USER_DEPRECATED);
             }
         }
 

+ 1 - 7
src/Console/ConfigurationResolver.php

@@ -677,13 +677,7 @@ final class ConfigurationResolver
                     ? sprintf(' and will be removed in version %d.0.', Application::getMajorVersion() + 1)
                     : sprintf('. Use %s instead.', str_replace('`', '"', Utils::naturalLanguageJoinWithBackticks($successors)));
 
-                $message = "Rule \"{$fixerName}\" is deprecated{$messageEnd}";
-
-                if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
-                    throw new \RuntimeException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
-                }
-
-                @trigger_error($message, E_USER_DEPRECATED);
+                Utils::triggerDeprecation("Rule \"{$fixerName}\" is deprecated{$messageEnd}");
             }
         }
     }

+ 2 - 1
src/FixerConfiguration/FixerConfigurationResolver.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\FixerConfiguration;
 
+use PhpCsFixer\Utils;
 use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
 use Symfony\Component\OptionsResolver\OptionsResolver;
 
@@ -69,7 +70,7 @@ final class FixerConfigurationResolver implements FixerConfigurationResolverInte
                         throw new InvalidOptionsException(sprintf('Aliased option "%s"/"%s" is passed multiple times.', $name, $alias));
                     }
 
-                    @trigger_error(sprintf('Option "%s" is deprecated, use "%s" instead.', $alias, $name), E_USER_DEPRECATED);
+                    Utils::triggerDeprecation(sprintf('Option "%s" is deprecated, use "%s" instead.', $alias, $name));
 
                     $options[$name] = $options[$alias];
                     unset($options[$alias]);

+ 12 - 0
src/Utils.php

@@ -169,4 +169,16 @@ final class Utils
 
         return $last;
     }
+
+    /**
+     * Handle triggering deprecation error.
+     */
+    public static function triggerDeprecation(string $message, string $exceptionClass = \RuntimeException::class): void
+    {
+        if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
+            throw new $exceptionClass("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
+        }
+
+        @trigger_error($message, E_USER_DEPRECATED);
+    }
 }

+ 31 - 0
tests/AutoReview/ProjectCodeTest.php

@@ -20,6 +20,7 @@ use PhpCsFixer\Preg;
 use PhpCsFixer\Tests\TestCase;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;
+use PhpCsFixer\Utils;
 use Symfony\Component\Finder\Finder;
 use Symfony\Component\Finder\SplFileInfo;
 
@@ -472,6 +473,36 @@ final class ProjectCodeTest extends TestCase
         static::assertNull($tokens->getNextNonWhitespace($classyEndIndex), sprintf('File "%s" should only contains a single classy.', $file));
     }
 
+    /**
+     * @dataProvider provideSrcClassCases
+     *
+     * @param string $className
+     */
+    public function testThereIsNoTriggerErrorUsedDirectly($className): void
+    {
+        if (Utils::class === $className) {
+            $this->addToAssertionCount(1); // This is where "trigger_error" should be
+
+            return;
+        }
+
+        $rc = new \ReflectionClass($className);
+        $tokens = Tokens::fromCode(file_get_contents($rc->getFileName()));
+
+        $triggerErrors = array_filter(
+            $tokens->toArray(),
+            static function (Token $token) {
+                return $token->equals([T_STRING, 'trigger_error'], false);
+            }
+        );
+
+        static::assertCount(
+            0,
+            $triggerErrors,
+            sprintf('Class "%s" must not use "trigger_error", it shall use "Util::triggerDeprecation" instead.', $className)
+        );
+    }
+
     public function provideSrcClassCases()
     {
         return array_map(

+ 25 - 0
tests/UtilsTest.php

@@ -17,6 +17,7 @@ namespace PhpCsFixer\Tests;
 use PhpCsFixer\Fixer\FixerInterface;
 use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Utils;
+use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
 
 /**
  * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
@@ -29,6 +30,8 @@ use PhpCsFixer\Utils;
  */
 final class UtilsTest extends TestCase
 {
+    use ExpectDeprecationTrait;
+
     /**
      * @param string $expected Camel case string
      * @param string $input    Input string
@@ -279,6 +282,28 @@ final class UtilsTest extends TestCase
         ];
     }
 
+    /**
+     * @group legacy
+     */
+    public function testTriggerDeprecationWhenFutureModeIsOff(): void
+    {
+        putenv('PHP_CS_FIXER_FUTURE_MODE=0');
+
+        $this->expectDeprecation('The message');
+
+        Utils::triggerDeprecation('The message', \DomainException::class);
+    }
+
+    public function testTriggerDeprecationWhenFutureModeIsOn(): void
+    {
+        putenv('PHP_CS_FIXER_FUTURE_MODE=1');
+
+        $this->expectException(\DomainException::class);
+        $this->expectExceptionMessage('The message');
+
+        Utils::triggerDeprecation('The message', \DomainException::class);
+    }
+
     private function createFixerDouble(string $name, int $priority)
     {
         $fixer = $this->prophesize(FixerInterface::class);