From e5093597a6bbb91195472bef01747bb4e7e26725 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Mon, 16 Oct 2023 13:05:23 +1100 Subject: [PATCH] Fixed static lint code issues. --- .circleci/config.yml | 2 +- phpstan.neon | 9 +------ .../ScreenshotContextInitializer.php | 14 +++++----- .../Context/ScreenshotAwareContext.php | 4 +-- .../Context/ScreenshotContext.php | 26 +++++++++++-------- .../BehatScreenshotExtension.php | 26 ++++++++++++------- tests/behat/bootstrap/FeatureContext.php | 5 ++-- tests/behat/bootstrap/ScreenshotTrait.php | 21 ++++++++------- 8 files changed, 56 insertions(+), 51 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2f9cd89..76d4925 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,7 +23,7 @@ jobs: command: docker compose exec phpserver composer install --ansi --no-suggest - run: name: Lint code. - command: docker compose exec phpserver composer lint || true + command: docker compose exec phpserver composer lint - run: name: Run tests. command: docker compose exec phpserver composer test diff --git a/phpstan.neon b/phpstan.neon index 24bf863..2651b39 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -13,11 +13,4 @@ parameters: excludePaths: - vendor/* - node_modules/* - - ignoreErrors: - - - # Since tests and data providers do not have to have parameter docblocks, - # it is not possible to specify the type of the parameter, so we ignore - # this error. - message: '#.*no value type specified in iterable type array.#' - path: tests/phpunit/* + - tests/behat/bootstrap/BehatCliContext.php diff --git a/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php b/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php index e9367c7..7d84ca4 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/Initializer/ScreenshotContextInitializer.php @@ -13,7 +13,7 @@ use Symfony\Component\Finder\Finder; /** - * Class ScreenshotContextInitializer + * Class ScreenshotContextInitializer. */ class ScreenshotContextInitializer implements ContextInitializer { @@ -58,10 +58,10 @@ class ScreenshotContextInitializer implements ContextInitializer * * @param string $dir Screenshot dir. * @param bool $fail Screenshot when fail. - * @param bool $failPrefix File name prefix for a failed test. + * @param string $failPrefix File name prefix for a failed test. * @param bool $purge Purge dir before start script. */ - public function __construct($dir, $fail, $failPrefix, $purge) + public function __construct(string $dir, bool $fail, string $failPrefix, bool $purge) { $this->needsPurging = true; $this->dir = $dir; @@ -73,7 +73,7 @@ public function __construct($dir, $fail, $failPrefix, $purge) /** * {@inheritdoc} */ - public function initializeContext(Context $context) + public function initializeContext(Context $context): void { if ($context instanceof ScreenshotAwareContext) { $dir = $this->resolveScreenshotDir(); @@ -91,7 +91,7 @@ public function initializeContext(Context $context) * @param string $dir * Directory to purge files in. */ - protected function purgeFilesInDir($dir) + protected function purgeFilesInDir(string $dir): void { $fs = new Filesystem(); $finder = new Finder(); @@ -106,7 +106,7 @@ protected function purgeFilesInDir($dir) * @return string * Path to the screenshots directory. */ - protected function resolveScreenshotDir() + protected function resolveScreenshotDir(): string { $dir = getenv('BEHAT_SCREENSHOT_DIR'); if (!empty($dir)) { @@ -122,7 +122,7 @@ protected function resolveScreenshotDir() * @return bool * TRUE if should purge, FALSE otherwise. */ - protected function shouldPurge() + protected function shouldPurge(): bool { return getenv('BEHAT_SCREENSHOT_PURGE') || $this->purge; } diff --git a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotAwareContext.php b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotAwareContext.php index 0b2ff34..66f36fe 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotAwareContext.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotAwareContext.php @@ -20,9 +20,9 @@ interface ScreenshotAwareContext extends Context * * @param string $dir Directory to store screenshots. * @param bool $fail Create screenshots on fail. - * @param bool $failPrefix File name prefix for a failed test. + * @param string $failPrefix File name prefix for a failed test. * * @return $this */ - public function setScreenshotParameters($dir, $fail, $failPrefix); + public function setScreenshotParameters(string $dir, bool $fail, string $failPrefix): static; } diff --git a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php index ab269cd..fa8d5ed 100644 --- a/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php +++ b/src/DrevOps/BehatScreenshotExtension/Context/ScreenshotContext.php @@ -61,7 +61,7 @@ class ScreenshotContext extends RawMinkContext implements SnippetAcceptingContex /** * {@inheritdoc} */ - public function setScreenshotParameters($dir, $fail, $failPrefix) + public function setScreenshotParameters(string $dir, bool $fail, string $failPrefix): static { $this->dir = $dir; $this->fail = $fail; @@ -77,7 +77,7 @@ public function setScreenshotParameters($dir, $fail, $failPrefix) * * @BeforeScenario */ - public function beforeScenarioInit(BeforeScenarioScope $scope) + public function beforeScenarioInit(BeforeScenarioScope $scope): void { if ($scope->getScenario()->hasTag('javascript')) { $driver = $this->getSession()->getDriver(); @@ -98,9 +98,13 @@ public function beforeScenarioInit(BeforeScenarioScope $scope) * * @BeforeStep */ - public function beforeStepInit(BeforeStepScope $scope) + public function beforeStepInit(BeforeStepScope $scope): void { - $this->featureFile = $scope->getFeature()->getFile(); + $featureFile = $scope->getFeature()->getFile(); + if (!$featureFile) { + throw new \RuntimeException('Feature file not found.'); + } + $this->featureFile = $featureFile; $this->stepLine = $scope->getStep()->getLine(); } @@ -111,7 +115,7 @@ public function beforeStepInit(BeforeStepScope $scope) * * @AfterStep */ - public function printLastResponseOnError(AfterStepScope $event) + public function printLastResponseOnError(AfterStepScope $event): void { if ($this->fail && !$event->getTestResult()->isPassed()) { $this->iSaveScreenshot(true); @@ -129,7 +133,7 @@ public function printLastResponseOnError(AfterStepScope $event) * @When save screenshot * @When I save screenshot */ - public function iSaveScreenshot($fail = false) + public function iSaveScreenshot($fail = false): void { $driver = $this->getSession()->getDriver(); @@ -169,14 +173,14 @@ public function iSaveScreenshot($fail = false) * @When save :width x :height screenshot * @When I save :width x :height screenshot */ - public function iSaveSizedScreenshot($width = 1440, $height = 900) + public function iSaveSizedScreenshot(string|int $width = 1440, string|int $height = 900): void { try { $this->getSession()->resizeWindow((int) $width, (int) $height, 'current'); } catch (UnsupportedDriverActionException $exception) { // Nothing to do here - drivers without resize support may proceed. } - $this->iSaveScreenshot(false); + $this->iSaveScreenshot(); } /** @@ -187,7 +191,7 @@ public function iSaveSizedScreenshot($width = 1440, $height = 900) * @param string $data * Data to write into a file. */ - protected function saveScreenshotData($filename, $data) + protected function saveScreenshotData(string $filename, string $data): void { $this->prepareDir($this->dir); file_put_contents($this->dir.DIRECTORY_SEPARATOR.$filename, $data); @@ -198,7 +202,7 @@ protected function saveScreenshotData($filename, $data) * * @param string $dir Name of preparing directory. */ - protected function prepareDir($dir) + protected function prepareDir(string $dir): void { $fs = new Filesystem(); $fs->mkdir($dir, 0755); @@ -214,7 +218,7 @@ protected function prepareDir($dir) * * @return string Unique file name. */ - protected function makeFileName($ext, $prefix = '') + protected function makeFileName(string $ext, string $prefix = ''): string { return sprintf('%01.2f.%s%s_%s.%s', microtime(true), $prefix, basename($this->featureFile), $this->stepLine, $ext); } diff --git a/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php b/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php index 70e5593..d8d9430 100644 --- a/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php +++ b/src/DrevOps/BehatScreenshotExtension/ServiceContainer/BehatScreenshotExtension.php @@ -11,6 +11,8 @@ use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface; use Behat\Testwork\ServiceContainer\ExtensionManager; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; +use Symfony\Component\Config\Definition\Builder\NodeBuilder; +use Symfony\Component\Config\Definition\Builder\NodeParentInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -28,14 +30,14 @@ class BehatScreenshotExtension implements ExtensionInterface /** * {@inheritdoc} */ - public function process(ContainerBuilder $container) + public function process(ContainerBuilder $container): void { } /** * {@inheritdoc} */ - public function getConfigKey() + public function getConfigKey(): string { return self::MOD_ID; } @@ -43,26 +45,30 @@ public function getConfigKey() /** * {@inheritdoc} */ - public function initialize(ExtensionManager $extensionManager) + public function initialize(ExtensionManager $extensionManager): void { } /** * {@inheritdoc} */ - public function configure(ArrayNodeDefinition $builder) + public function configure(ArrayNodeDefinition $builder): void { - $builder->children() - ->scalarNode('dir')->cannotBeEmpty()->defaultValue('%paths.base%/screenshots')->end() - ->scalarNode('fail')->cannotBeEmpty()->defaultValue(true)->end() - ->scalarNode('fail_prefix')->cannotBeEmpty()->defaultValue('failed_')->end() - ->scalarNode('purge')->cannotBeEmpty()->defaultValue(false)->end(); + $definitionChildren = $builder->children(); + if ($definitionChildren instanceof NodeBuilder) { + // @phpstan-ignore-next-line + $definitionChildren + ->scalarNode('dir')->cannotBeEmpty()->defaultValue('%paths.base%/screenshots')->end() + ->scalarNode('fail')->cannotBeEmpty()->defaultValue(true)->end() + ->scalarNode('fail_prefix')->cannotBeEmpty()->defaultValue('failed_')->end() + ->scalarNode('purge')->cannotBeEmpty()->defaultValue(false)->end(); + } } /** * {@inheritdoc} */ - public function load(ContainerBuilder $container, array $config) + public function load(ContainerBuilder $container, array $config): void { $definition = new Definition('DrevOps\BehatScreenshotExtension\Context\Initializer\ScreenshotContextInitializer', [ $config['dir'], diff --git a/tests/behat/bootstrap/FeatureContext.php b/tests/behat/bootstrap/FeatureContext.php index dedf18a..4be7e2a 100644 --- a/tests/behat/bootstrap/FeatureContext.php +++ b/tests/behat/bootstrap/FeatureContext.php @@ -13,14 +13,15 @@ */ class FeatureContext extends MinkContext implements Context { + use ScreenshotTrait; /** * FeatureContext constructor. * - * @param array $parameters Array of parameters from config. + * @param array $parameters Array of parameters from config. */ - public function __construct($parameters) + public function __construct(array $parameters) { $this->screenshotInitParams($parameters); } diff --git a/tests/behat/bootstrap/ScreenshotTrait.php b/tests/behat/bootstrap/ScreenshotTrait.php index 7037d35..c6e0262 100644 --- a/tests/behat/bootstrap/ScreenshotTrait.php +++ b/tests/behat/bootstrap/ScreenshotTrait.php @@ -22,9 +22,9 @@ trait ScreenshotTrait /** * Init test parameters. * - * @param array $parameters Array of parameters from config. + * @param array $parameters Array of parameters from config. */ - public function screenshotInitParams($parameters) + public function screenshotInitParams(array $parameters): void { if (getenv('BEHAT_SCREENSHOT_DIR')) { $this->screenshotDir = getenv('BEHAT_SCREENSHOT_DIR'); @@ -41,7 +41,7 @@ public function screenshotInitParams($parameters) * @Given /^(?:|I )am on (?:|the )screenshot test page$/ * @Given /^(?:|I )go to (?:|the )screenshot test page$/ */ - public function goToScreenshotTestPage() + public function goToScreenshotTestPage(): void { $this->visitPath('screenshot.html'); } @@ -53,7 +53,7 @@ public function goToScreenshotTestPage() * * @Given /^file wildcard "([^"]*)" should exist$/ */ - public function assertFileShouldExist($wildcard) + public function assertFileShouldExist(string $wildcard): void { $wildcard = $this->screenshotDir.DIRECTORY_SEPARATOR.$wildcard; $matches = glob($wildcard); @@ -70,7 +70,7 @@ public function assertFileShouldExist($wildcard) * * @Given /^file wildcard "([^"]*)" should not exist$/ */ - public function assertFileShouldNotExist($wildcard) + public function assertFileShouldNotExist(string $wildcard): void { $wildcard = $this->screenshotDir.DIRECTORY_SEPARATOR.$wildcard; $matches = glob($wildcard); @@ -85,11 +85,12 @@ public function assertFileShouldNotExist($wildcard) * * @Given I remove all files from screenshot directory */ - public function emptyScreenshotDirectory() + public function emptyScreenshotDirectory(): void { - array_map( - 'unlink', - glob($this->screenshotDir.DIRECTORY_SEPARATOR.'/*') - ); + $files = glob($this->screenshotDir.DIRECTORY_SEPARATOR.'/*'); + + if (!empty($files)) { + array_map('unlink', $files); + } } }