From 9fe8bf0e015addc78fca658be171c4af4f209b3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Sun, 24 Mar 2019 07:52:11 +0100 Subject: [PATCH] Enhancement: Enable static_lambda fixer (#674) * Enhancement: Enable static_lambda fixer * Fix: Run 'make cs-fix' --- .php_cs.dist | 1 + src/Config/InfectionConfig.php | 2 +- .../ValueProvider/ExcludeDirsProvider.php | 4 ++-- .../PhpUnitCustomExecutablePathProvider.php | 2 +- src/Config/ValueProvider/TimeoutProvider.php | 2 +- src/Console/InfectionContainer.php | 24 +++++++++---------- src/Differ/DiffColorizer.php | 2 +- src/Logger/FileLogger.php | 2 +- .../Coverage/CodeCoverageData.php | 2 +- .../Config/Builder/MutationConfigBuilder.php | 2 +- tests/AutoReview/ProjectCodeTest.php | 4 ++-- .../ValueProvider/ExcludeDirsProviderTest.php | 2 +- .../Generator/MutationsGeneratorTest.php | 8 +++---- tests/Mutator/Util/MutatorProfileTest.php | 2 +- .../PerformanceLoggerSubscriberTest.php | 2 +- .../Process/Runner/InitialTestsRunnerTest.php | 2 +- 16 files changed, 32 insertions(+), 31 deletions(-) diff --git a/.php_cs.dist b/.php_cs.dist index 7eb1890bf..22290b040 100644 --- a/.php_cs.dist +++ b/.php_cs.dist @@ -63,6 +63,7 @@ return \PhpCsFixer\Config::create() 'style' => 'prefix', 'case' => 'snake', ], + 'static_lambda' => true, 'strict_comparison' => true, 'yoda_style' => false, ]) diff --git a/src/Config/InfectionConfig.php b/src/Config/InfectionConfig.php index 91843a6b7..152e19d75 100644 --- a/src/Config/InfectionConfig.php +++ b/src/Config/InfectionConfig.php @@ -189,7 +189,7 @@ private function getExcludedDirsByPattern(string $originalExcludedDir) $excludedDirs = array_merge( $excludedDirs, array_map( - function ($excludeDir) use ($srcDir) { + static function ($excludeDir) use ($srcDir) { return ltrim( substr_replace($excludeDir, '', 0, \strlen($srcDir)), \DIRECTORY_SEPARATOR diff --git a/src/Config/ValueProvider/ExcludeDirsProvider.php b/src/Config/ValueProvider/ExcludeDirsProvider.php index 5c973413c..5fe9785d1 100644 --- a/src/Config/ValueProvider/ExcludeDirsProvider.php +++ b/src/Config/ValueProvider/ExcludeDirsProvider.php @@ -103,7 +103,7 @@ public function get(InputInterface $input, OutputInterface $output, array $dirsI $globDirs = array_filter(glob($sourceDirs[0] . '/*'), 'is_dir'); $autocompleteValues = array_map( - function (string $dir) use ($sourceDirs) { + static function (string $dir) use ($sourceDirs) { return str_replace($sourceDirs[0] . '/', '', $dir); }, $globDirs @@ -125,7 +125,7 @@ function (string $dir) use ($sourceDirs) { private function getValidator(Locator $locator) { - return function ($answer) use ($locator) { + return static function ($answer) use ($locator) { if (!$answer || strpos($answer, '*') !== false) { return $answer; } diff --git a/src/Config/ValueProvider/PhpUnitCustomExecutablePathProvider.php b/src/Config/ValueProvider/PhpUnitCustomExecutablePathProvider.php index ece3177b7..7b594c1f5 100644 --- a/src/Config/ValueProvider/PhpUnitCustomExecutablePathProvider.php +++ b/src/Config/ValueProvider/PhpUnitCustomExecutablePathProvider.php @@ -95,7 +95,7 @@ public function get(InputInterface $input, OutputInterface $output) private function getValidator(): \Closure { - return function ($answerPath) { + return static function ($answerPath) { $answerPath = $answerPath ? trim($answerPath) : $answerPath; if (!$answerPath || !file_exists($answerPath)) { diff --git a/src/Config/ValueProvider/TimeoutProvider.php b/src/Config/ValueProvider/TimeoutProvider.php index 4c81b073b..aa69d8642 100644 --- a/src/Config/ValueProvider/TimeoutProvider.php +++ b/src/Config/ValueProvider/TimeoutProvider.php @@ -70,7 +70,7 @@ public function get(InputInterface $input, OutputInterface $output) $questionText = $this->consoleHelper->getQuestion('Single test suite timeout in seconds', InfectionConfig::PROCESS_TIMEOUT_SECONDS); $timeoutQuestion = new Question($questionText, InfectionConfig::PROCESS_TIMEOUT_SECONDS); - $timeoutQuestion->setValidator(function ($answer) { + $timeoutQuestion->setValidator(static function ($answer) { if (!$answer || !is_numeric($answer) || (int) $answer <= 0) { throw new \RuntimeException('Timeout should be an integer greater than 0'); } diff --git a/src/Console/InfectionContainer.php b/src/Console/InfectionContainer.php index 47fd9896a..e5a675bb6 100644 --- a/src/Console/InfectionContainer.php +++ b/src/Console/InfectionContainer.php @@ -97,7 +97,7 @@ public function __construct(array $values = []) return $this->getInfectionConfig()->getPhpUnitConfigDir(); }; - $this['filesystem'] = function (): Filesystem { + $this['filesystem'] = static function (): Filesystem { return new Filesystem(); }; @@ -153,13 +153,13 @@ public function __construct(array $values = []) ); }; - $this['differ'] = function (): Differ { + $this['differ'] = static function (): Differ { return new Differ( new BaseDiffer() ); }; - $this['dispatcher'] = function (): EventDispatcherInterface { + $this['dispatcher'] = static function (): EventDispatcherInterface { return new EventDispatcher(); }; @@ -173,7 +173,7 @@ public function __construct(array $values = []) ); }; - $this['diff.colorizer'] = function (): DiffColorizer { + $this['diff.colorizer'] = static function (): DiffColorizer { return new DiffColorizer(); }; @@ -183,11 +183,11 @@ public function __construct(array $values = []) ); }; - $this['version.parser'] = function (): VersionParser { + $this['version.parser'] = static function (): VersionParser { return new VersionParser(); }; - $this['lexer'] = function (): Lexer { + $this['lexer'] = static function (): Lexer { return new Lexer\Emulative([ 'usedAttributes' => [ 'comments', 'startLine', 'endLine', 'startTokenPos', 'endTokenPos', 'startFilePos', 'endFilePos', @@ -199,7 +199,7 @@ public function __construct(array $values = []) return (new ParserFactory())->create(ParserFactory::PREFER_PHP7, $this['lexer']); }; - $this['pretty.printer'] = function (): Standard { + $this['pretty.printer'] = static function (): Standard { return new Standard(); }; @@ -209,19 +209,19 @@ public function __construct(array $values = []) return (new MutatorsGenerator($mutatorConfig))->generate(); }; - $this['metrics'] = function (): MetricsCalculator { + $this['metrics'] = static function (): MetricsCalculator { return new MetricsCalculator(); }; - $this['timer'] = function (): Timer { + $this['timer'] = static function (): Timer { return new Timer(); }; - $this['time.formatter'] = function (): TimeFormatter { + $this['time.formatter'] = static function (): TimeFormatter { return new TimeFormatter(); }; - $this['memory.formatter'] = function (): MemoryFormatter { + $this['memory.formatter'] = static function (): MemoryFormatter { return new MemoryFormatter(); }; @@ -254,7 +254,7 @@ public function buildDynamicDependencies(InputInterface $input): void : sprintf('%s/%s', getcwd(), $existingCoveragePath); }; - $this['coverage.checker'] = function () use ($input): CoverageRequirementChecker { + $this['coverage.checker'] = static function () use ($input): CoverageRequirementChecker { return new CoverageRequirementChecker( \strlen(trim($input->getOption('coverage'))) > 0, $input->getOption('initial-tests-php-options') diff --git a/src/Differ/DiffColorizer.php b/src/Differ/DiffColorizer.php index 3e0fe9e85..42ddcec36 100644 --- a/src/Differ/DiffColorizer.php +++ b/src/Differ/DiffColorizer.php @@ -42,7 +42,7 @@ class DiffColorizer { public function colorize(string $diff): string { - $lines = array_map(function (string $line) { + $lines = array_map(static function (string $line) { if (0 === strpos($line, '-')) { return sprintf('%s', $line); } diff --git a/src/Logger/FileLogger.php b/src/Logger/FileLogger.php index ad6aff275..9f7e42398 100644 --- a/src/Logger/FileLogger.php +++ b/src/Logger/FileLogger.php @@ -115,7 +115,7 @@ abstract protected function getLogLines(): array; */ final protected function sortProcesses(array &$processes): void { - usort($processes, function (MutantProcessInterface $a, MutantProcessInterface $b): int { + usort($processes, static function (MutantProcessInterface $a, MutantProcessInterface $b): int { if ($a->getOriginalFilePath() === $b->getOriginalFilePath()) { return $a->getOriginalStartingLine() <=> $b->getOriginalStartingLine(); } diff --git a/src/TestFramework/Coverage/CodeCoverageData.php b/src/TestFramework/Coverage/CodeCoverageData.php index 16d1171ca..8de22c062 100644 --- a/src/TestFramework/Coverage/CodeCoverageData.php +++ b/src/TestFramework/Coverage/CodeCoverageData.php @@ -90,7 +90,7 @@ public function hasTests(string $filePath): bool $coveredLineTestMethods = array_filter( $coverageData[$filePath]['byLine'], - function ($testMethods) { + static function ($testMethods) { return \count($testMethods) > 0; } ); diff --git a/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php b/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php index b2d51dcf1..b79441252 100644 --- a/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php +++ b/src/TestFramework/PhpUnit/Config/Builder/MutationConfigBuilder.php @@ -189,7 +189,7 @@ private function addTestSuiteWithFilteredTestFiles(array $coverageTests, \DOMDoc // sort tests to run the fastest first usort( $uniqueCoverageTests, - function (array $a, array $b) { + static function (array $a, array $b) { return $a['time'] <=> $b['time']; } ); diff --git a/tests/AutoReview/ProjectCodeTest.php b/tests/AutoReview/ProjectCodeTest.php index 972fad4a4..be7f3cb83 100644 --- a/tests/AutoReview/ProjectCodeTest.php +++ b/tests/AutoReview/ProjectCodeTest.php @@ -349,7 +349,7 @@ public function test_src_classes_do_not_expose_public_properties(string $classNa * we're exteding from. E.g. we can't change Symfony\Component\Process\Process to not have * a public propery it has. */ - $properties = array_filter($properties, function (\ReflectionProperty $property) use ($className) { + $properties = array_filter($properties, static function (\ReflectionProperty $property) use ($className) { return $property->class === $className; }); @@ -489,7 +489,7 @@ static function (SplFileInfo $file) { private function getConcreteSrcClasses(): array { return array_filter($this->getSrcClasses(), - function ($class) { + static function ($class) { $rc = new \ReflectionClass($class); return !$rc->isInterface() && !$rc->isAbstract() && !$rc->isTrait(); diff --git a/tests/Config/ValueProvider/ExcludeDirsProviderTest.php b/tests/Config/ValueProvider/ExcludeDirsProviderTest.php index ec649afce..565d9fa52 100644 --- a/tests/Config/ValueProvider/ExcludeDirsProviderTest.php +++ b/tests/Config/ValueProvider/ExcludeDirsProviderTest.php @@ -134,7 +134,7 @@ public function test_passes_when_correct_dir_typed(): void public function excludeDirsProvider() { return array_map( - function (string $excludedRootDir) { + static function (string $excludedRootDir) { return [$excludedRootDir, [$excludedRootDir, 'src']]; }, ExcludeDirsProvider::EXCLUDED_ROOT_DIRS diff --git a/tests/Mutant/Generator/MutationsGeneratorTest.php b/tests/Mutant/Generator/MutationsGeneratorTest.php index e18ff8f64..c7c10c60a 100644 --- a/tests/Mutant/Generator/MutationsGeneratorTest.php +++ b/tests/Mutant/Generator/MutationsGeneratorTest.php @@ -229,19 +229,19 @@ private function createMutationGenerator( $mutatorConfig = $mutatorConfig ?? new MutatorConfig([]); - $container[Plus::class] = function () use ($mutatorConfig) { + $container[Plus::class] = static function () use ($mutatorConfig) { return new Plus($mutatorConfig); }; - $container[PublicVisibility::class] = function () use ($mutatorConfig) { + $container[PublicVisibility::class] = static function () use ($mutatorConfig) { return new PublicVisibility($mutatorConfig); }; - $container[TrueValue::class] = function () use ($mutatorConfig) { + $container[TrueValue::class] = static function () use ($mutatorConfig) { return new TrueValue($mutatorConfig); }; - $container[DecrementInteger::class] = function (Container $c) use ($mutatorConfig) { + $container[DecrementInteger::class] = static function (Container $c) use ($mutatorConfig) { return new DecrementInteger($mutatorConfig); }; diff --git a/tests/Mutator/Util/MutatorProfileTest.php b/tests/Mutator/Util/MutatorProfileTest.php index 0203a30e2..a34dfb4f9 100644 --- a/tests/Mutator/Util/MutatorProfileTest.php +++ b/tests/Mutator/Util/MutatorProfileTest.php @@ -144,7 +144,7 @@ private function getMutatorProfileConstants(): array return array_filter( $reflectionClass->getConstants(), - function (string $constantName) use ($excludedConstants): bool { + static function (string $constantName) use ($excludedConstants): bool { return !\in_array($constantName, $excludedConstants, true); }, ARRAY_FILTER_USE_KEY diff --git a/tests/Performance/Listener/PerformanceLoggerSubscriberTest.php b/tests/Performance/Listener/PerformanceLoggerSubscriberTest.php index db6ea6a4c..a1b69e974 100644 --- a/tests/Performance/Listener/PerformanceLoggerSubscriberTest.php +++ b/tests/Performance/Listener/PerformanceLoggerSubscriberTest.php @@ -65,7 +65,7 @@ public function test_it_reacts_on_application_execution_events(): void { $this->output->expects($this->once()) ->method('writeln') - ->with($this->callback(function ($parameter) { + ->with($this->callback(static function ($parameter) { return \is_array($parameter) && '' === $parameter[0] && 0 === strpos($parameter[1], 'Time:'); })); diff --git a/tests/Process/Runner/InitialTestsRunnerTest.php b/tests/Process/Runner/InitialTestsRunnerTest.php index 33ae29ca7..ef0a0d45f 100644 --- a/tests/Process/Runner/InitialTestsRunnerTest.php +++ b/tests/Process/Runner/InitialTestsRunnerTest.php @@ -57,7 +57,7 @@ public function test_it_dispatches_events(): void $process->expects($this->once()) ->method('run') - ->with($this->callback(function ($processCallback): bool { + ->with($this->callback(static function ($processCallback): bool { $processCallback(Process::OUT); return true;