Browse Source

Improve deprecation messages

Julien Falque 6 years ago
parent
commit
515b34bcd7

+ 15 - 9
src/AbstractFixer.php

@@ -118,14 +118,13 @@ abstract class AbstractFixer implements FixerInterface, DefinedFixerInterface
         }
 
         if (null === $configuration) {
+            $message = 'Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.';
+
             if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
-                throw new \InvalidArgumentException('Parameter must not be `null`. This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.');
+                throw new \InvalidArgumentException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
             }
 
-            @trigger_error(
-                'Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.',
-                E_USER_DEPRECATED
-            );
+            @trigger_error($message, E_USER_DEPRECATED);
 
             $configuration = [];
         }
@@ -137,12 +136,19 @@ abstract class AbstractFixer implements FixerInterface, DefinedFixerInterface
 
             $name = $option->getName();
             if (array_key_exists($name, $configuration)) {
-                @trigger_error(sprintf(
-                    'Option "%s" is deprecated and will be removed in %d.0. %s',
+                $message = sprintf(
+                    'Option "%s" for rule "%s" is deprecated and will be removed in version %d.0. %s',
                     $name,
-                    Application::VERSION + 1,
+                    $this->getName(),
+                    (int) Application::VERSION + 1,
                     str_replace('`', '"', $option->getDeprecationMessage())
-                ), E_USER_DEPRECATED);
+                );
+
+                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);
             }
         }
 

+ 29 - 30
src/Console/ConfigurationResolver.php

@@ -345,25 +345,6 @@ final class ConfigurationResolver
                     throw new InvalidConfigurationException(sprintf('The rules contain risky fixers (%s), but they are not allowed to run. Perhaps you forget to use --allow-risky option?', implode(', ', $riskyFixers)));
                 }
             }
-
-            foreach ($this->fixers as $fixer) {
-                if ($fixer instanceof DeprecatedFixerInterface) {
-                    $successors = $fixer->getSuccessorsNames();
-                    $message = sprintf(
-                        'Fixer `%s` is deprecated%s',
-                        $fixer->getName(),
-                        [] === $successors
-                            ? ' and will be removed on next major version.'
-                            : sprintf(', use %s instead.', Utils::naturalLanguageJoinWithBackticks($successors))
-                    );
-
-                    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);
-                }
-            }
         }
 
         return $this->fixers;
@@ -445,14 +426,13 @@ final class ConfigurationResolver
                         implode('", "', $progressTypes)
                     ));
                 } elseif (in_array($progressType, ['estimating', 'estimating-max', 'run-in'], true)) {
+                    $message = 'Passing `estimating`, `estimating-max` or `run-in` is deprecated and will not be supported in 3.0, use `none` or `dots` instead.';
+
                     if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
-                        throw new \InvalidArgumentException('Passing `estimating`, `estimating-max` or `run-in` is deprecated and will not be supported in 3.0, use `none` or `dots` instead. This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.');
+                        throw new \InvalidArgumentException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
                     }
 
-                    @trigger_error(
-                        'Passing `estimating`, `estimating-max` or `run-in` is deprecated and will not be supported in 3.0, use `none` or `dots` instead.',
-                        E_USER_DEPRECATED
-                    );
+                    @trigger_error($message, E_USER_DEPRECATED);
                 }
 
                 $this->progress = $progressType;
@@ -749,10 +729,12 @@ final class ConfigurationResolver
         /** @var string[] $configuredFixers */
         $configuredFixers = array_keys($ruleSet->getRules());
 
+        $fixers = $this->createFixerFactory()->getFixers();
+
         /** @var string[] $availableFixers */
         $availableFixers = array_map(static function (FixerInterface $fixer) {
             return $fixer->getName();
-        }, $this->createFixerFactory()->getFixers());
+        }, $fixers);
 
         $unknownFixers = array_diff(
             $configuredFixers,
@@ -774,6 +756,24 @@ final class ConfigurationResolver
 
             throw new InvalidConfigurationException(substr($message, 0, -2).'.');
         }
+
+        foreach ($fixers as $fixer) {
+            $fixerName = $fixer->getName();
+            if (isset($rules[$fixerName]) && $fixer instanceof DeprecatedFixerInterface) {
+                $successors = $fixer->getSuccessorsNames();
+                $messageEnd = [] === $successors
+                    ? sprintf(' and will be removed in version %d.0.', (int) Application::VERSION + 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);
+            }
+        }
     }
 
     /**
@@ -917,14 +917,13 @@ final class ConfigurationResolver
             return false;
         }
 
+        $message = sprintf('Expected "yes" or "no" for option "%s", other values are deprecated and support will be removed in 3.0. Got "%s", this implicitly set the option to "false".', $optionName, $value);
+
         if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
-            throw new InvalidConfigurationException(sprintf('Expected "yes" or "no" for option "%s", got "%s". This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.', $optionName, $value));
+            throw new InvalidConfigurationException("{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
         }
 
-        @trigger_error(
-            sprintf('Expected "yes" or "no" for option "%s", other values are deprecated and support will be removed in 3.0. Got "%s", this implicitly set the option to "false".', $optionName, $value),
-            E_USER_DEPRECATED
-        );
+        @trigger_error($message, E_USER_DEPRECATED);
 
         return false;
     }

+ 1 - 1
src/Fixer/Alias/RandomApiMigrationFixer.php

@@ -162,6 +162,6 @@ final class RandomApiMigrationFixer extends AbstractFunctionReferenceFixer imple
                     'srand' => 'mt_srand',
                 ])
                 ->getOption(),
-        ]);
+        ], $this->getName());
     }
 }

+ 1 - 1
src/Fixer/ClassNotation/OrderedClassElementsFixer.php

@@ -276,7 +276,7 @@ class Example
                 ->setAllowedValues($this->supportedSortAlgorithms)
                 ->setDefault(self::SORT_NONE)
                 ->getOption(),
-        ]);
+        ], $this->getName());
     }
 
     /**

+ 1 - 1
src/Fixer/ClassNotation/SingleClassElementPerStatementFixer.php

@@ -104,7 +104,7 @@ final class Example
                 ->setAllowedTypes(['array'])
                 ->setAllowedValues([new AllowedValueSubset($values)])
                 ->getOption(),
-        ]);
+        ], $this->getName());
     }
 
     /**

+ 1 - 1
src/Fixer/ClassNotation/VisibilityRequiredFixer.php

@@ -119,7 +119,7 @@ class Sample
                 })
                 ->setDefault(['property', 'method'])
                 ->getOption(),
-        ]);
+        ], $this->getName());
     }
 
     /**

+ 1 - 1
src/Fixer/ControlStructure/NoUnneededControlParenthesesFixer.php

@@ -184,6 +184,6 @@ yield(2);
                     'yield',
                 ])
                 ->getOption(),
-        ]);
+        ], $this->getName());
     }
 }

+ 8 - 2
src/Fixer/Operator/BinaryOperatorSpacesFixer.php

@@ -474,11 +474,17 @@ $h = $i===  $j;
             }
         }
 
-        @trigger_error(sprintf(
+        $message = sprintf(
             'Given configuration is deprecated and will be removed in 3.0. Use configuration %s as replacement for %s.',
             HelpCommand::toString($newConfig),
             HelpCommand::toString($configuration)
-        ), E_USER_DEPRECATED);
+        );
+
+        if (getenv('PHP_CS_FIXER_FUTURE_MODE')) {
+            throw new InvalidFixerConfigurationException($this->getName(), "{$message} This check was performed as `PHP_CS_FIXER_FUTURE_MODE` env var is set.");
+        }
+
+        @trigger_error($message, E_USER_DEPRECATED);
 
         return $newConfig;
     }

+ 1 - 1
src/Fixer/PhpUnit/PhpUnitConstructFixer.php

@@ -129,7 +129,7 @@ $this->assertNotSame(null, $d);
                     'assertNotSame',
                 ])
                 ->getOption(),
-        ]);
+        ], $this->getName());
     }
 
     /**

+ 1 - 1
src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php

@@ -253,7 +253,7 @@ $this->assertTrue(is_readable($a));
                 ])
                 ->setDefault(PhpUnitTargetVersion::VERSION_5_0) // @TODO 3.x: change to `VERSION_NEWEST`
                 ->getOption(),
-        ]);
+        ], $this->getName());
     }
 
     /**

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