From 02b10a110d55e078151a2af16395501a0b7592aa Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Mon, 4 Nov 2024 22:47:20 +1100 Subject: [PATCH] Updated coding standards to use strict types. --- phpcs.xml | 10 ++-- rector.php | 8 ++-- .../ScreenshotContextInitializer.php | 2 + .../ScreenshotAwareContextInterface.php | 2 + .../Context/ScreenshotContext.php | 2 + .../BehatScreenshotExtension.php | 2 + tests/behat/bootstrap/BehatCliTrait.php | 48 +++++++++---------- tests/behat/bootstrap/FeatureContext.php | 7 +-- tests/behat/bootstrap/ScreenshotTrait.php | 5 +- 9 files changed, 46 insertions(+), 40 deletions(-) diff --git a/phpcs.xml b/phpcs.xml index b065969..25990b7 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -2,10 +2,14 @@ Custom PHPCS standard. - + + + + BehatCliContext\.php + @@ -13,7 +17,6 @@ - src tests/phpunit tests/behat/bootstrap @@ -30,8 +33,9 @@ *.Test\.php *.TestCase\.php *.test - + + tests/behat/bootstrap/BehatCliTrait.php diff --git a/rector.php b/rector.php index d4aa6f7..fe6a524 100644 --- a/rector.php +++ b/rector.php @@ -21,6 +21,7 @@ use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector; use Rector\Set\ValueObject\SetList; use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector; +use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ @@ -28,8 +29,7 @@ ]); $rectorConfig->sets([ - SetList::PHP_80, - SetList::PHP_81, + SetList::PHP_82, SetList::CODE_QUALITY, SetList::CODING_STYLE, SetList::DEAD_CODE, @@ -37,6 +37,8 @@ SetList::TYPE_DECLARATION, ]); + $rectorConfig->rule(DeclareStrictTypesRector::class); + $rectorConfig->skip([ // Rules added by Rector's rule sets. CountArrayToEmptyArrayComparisonRector::class, @@ -49,7 +51,7 @@ // Dependencies. '*/vendor/*', '*/node_modules/*', - '*/tests/*', + 'tests/behat/bootstrap/BehatCliContext.php', ]); $rectorConfig->fileExtensions([ diff --git a/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php b/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php index 37ab646..09bb27b 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php @@ -1,5 +1,7 @@ getFeature()->hasTag('behatcli')) { $traits = [ 'tests/behat/bootstrap/ScreenshotTrait.php' => 'ScreenshotTrait', @@ -28,15 +30,13 @@ public function behatCliBeforeScenario(BeforeScenarioScope $scope) { /** * @AfterScenario */ - public function behatCliAfterScenarioPrintOutput(AfterScenarioScope $scope) { - if ($scope->getFeature()->hasTag('behatcli')) { - if (static::behatCliIsDebug()) { - print "-------------------- OUTPUT START --------------------" . PHP_EOL; - print PHP_EOL; - print $this->getOutput(); - print PHP_EOL; - print "-------------------- OUTPUT FINISH -------------------" . PHP_EOL; - } + public function behatCliAfterScenarioPrintOutput(AfterScenarioScope $scope): void { + if ($scope->getFeature()->hasTag('behatcli') && static::behatCliIsDebug()) { + print "-------------------- OUTPUT START --------------------" . PHP_EOL; + print PHP_EOL; + print $this->getOutput(); + print PHP_EOL; + print "-------------------- OUTPUT FINISH -------------------" . PHP_EOL; } } @@ -49,7 +49,7 @@ public function behatCliAfterScenarioPrintOutput(AfterScenarioScope $scope) { * @return string * Path to written file. */ - public function behatCliWriteFeatureContextFile(array $traits = []) { + public function behatCliWriteFeatureContextFile(array $traits = []): string { $tokens = [ '{{USE_DECLARATION}}' => '', '{{USE_IN_CLASS}}' => '', @@ -127,7 +127,7 @@ public function throwTestException($message) { /** * @Given /^scenario steps(?: tagged with "([^"]*)")?:$/ */ - public function behatCliWriteScenarioSteps(PyStringNode $content, $tags = '') { + public function behatCliWriteScenarioSteps(PyStringNode $content, $tags = ''): void { $content = strtr((string) $content, ["'''" => '"""']); // Make sure that indentation in provided content is accurate. @@ -164,7 +164,7 @@ public function behatCliWriteScenarioSteps(PyStringNode $content, $tags = '') { /** * @Given some behat configuration */ - public function behatCliWriteBehatYml() { + public function behatCliWriteBehatYml(): void { $content = <<<'EOL' default: suites: @@ -194,7 +194,7 @@ public function behatCliWriteBehatYml() { /** * @Given screenshot context behat configuration with value: */ - public function behatCliWriteScreenshotContextBehatYml(PyStringNode $value) { + public function behatCliWriteScreenshotContextBehatYml(PyStringNode $value): void { $content = <<<'EOL' default: suites: @@ -226,7 +226,7 @@ public function behatCliWriteScreenshotContextBehatYml(PyStringNode $value) { /** * @Given screenshot fixture */ - public function behatCliWriteScreenshotFixture() { + public function behatCliWriteScreenshotFixture(): void { $filename = 'tests/behat/features/fixtures/screenshot.html'; $content = <<<'EOL' @@ -246,7 +246,7 @@ public function behatCliWriteScreenshotFixture() { /** * @Then it should fail with an error: */ - public function behatCliAssertFailWithError(PyStringNode $message) { + public function behatCliAssertFailWithError(PyStringNode $message): void { $this->itShouldFail('fail'); Assert::assertStringContainsString(trim((string) $message), $this->getOutput()); // Enforce \Exception for all assertion exceptions. Non-assertion @@ -258,7 +258,7 @@ public function behatCliAssertFailWithError(PyStringNode $message) { /** * @Then it should fail with an exception: */ - public function behatCliAssertFailWithException(PyStringNode $message) { + public function behatCliAssertFailWithException(PyStringNode $message): void { $this->itShouldFail('fail'); Assert::assertStringContainsString(trim((string) $message), $this->getOutput()); // Enforce \RuntimeException for all non-assertion exceptions. Assertion @@ -271,25 +271,25 @@ public function behatCliAssertFailWithException(PyStringNode $message) { * * @When :name environment variable is set to :value */ - public function behatCliSetEnvironmentVariable($name, $value) { + public function behatCliSetEnvironmentVariable($name, $value): void { $this->env[$name] = $value; } /** * Helper to print file comments. */ - protected static function behatCliPrintFileContents($filename, $title = '') { + protected static function behatCliPrintFileContents(string $filename, $title = '') { if (!is_readable($filename)) { throw new \RuntimeException(sprintf('Unable to access file "%s"', $filename)); } $content = file_get_contents($filename); - print "-------------------- $title START --------------------" . PHP_EOL; + print sprintf('-------------------- %s START --------------------', $title) . PHP_EOL; print $filename . PHP_EOL; print_r($content); print PHP_EOL; - print "-------------------- $title FINISH --------------------" . PHP_EOL; + print sprintf('-------------------- %s FINISH --------------------', $title) . PHP_EOL; } /** @@ -298,7 +298,7 @@ protected static function behatCliPrintFileContents($filename, $title = '') { * @return bool * TRUE to see debug messages for this trait. */ - protected static function behatCliIsDebug() { + protected static function behatCliIsDebug(): string|false { return getenv('BEHAT_CLI_DEBUG'); } @@ -310,7 +310,7 @@ protected static function behatCliIsDebug() { * * @Given /^behat cli file wildcard "([^"]*)" should exist$/ */ - public function behatCliAssertFileShouldExist($wildcard) { + public function behatCliAssertFileShouldExist($wildcard): void { $wildcard = $this->workingDir . DIRECTORY_SEPARATOR . $wildcard; $matches = glob($wildcard); @@ -329,7 +329,7 @@ public function behatCliAssertFileShouldExist($wildcard) { * * @Given /^behat cli file wildcard "([^"]*)" should not exist$/ */ - public function behatCliAssertFileShouldNotExist($wildcard) { + public function behatCliAssertFileShouldNotExist($wildcard): void { $wildcard = $this->workingDir . DIRECTORY_SEPARATOR . $wildcard; $matches = glob($wildcard); diff --git a/tests/behat/bootstrap/FeatureContext.php b/tests/behat/bootstrap/FeatureContext.php index 0650fea..b0ecb12 100644 --- a/tests/behat/bootstrap/FeatureContext.php +++ b/tests/behat/bootstrap/FeatureContext.php @@ -1,9 +1,6 @@