Skip to content

Commit

Permalink
Updated coding standards to use strict types.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Nov 4, 2024
1 parent a623319 commit 02b10a1
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 40 deletions.
10 changes: 7 additions & 3 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
<ruleset name="custom">
<description>Custom PHPCS standard.</description>

<!-- Coding standard. -->
<!-- Using Drupal coding standard as a base-fix. -->
<rule ref="Drupal">
<exclude name="Drupal.Commenting.ClassComment.Short"/>
</rule>
<!-- Checks that the strict_types has been declared. -->
<rule ref="Generic.PHP.RequireStrictTypes">
<exclude-pattern>BehatCliContext\.php</exclude-pattern>
</rule>

<!-- Show sniff codes in all reports -->
<arg value="s"/>
<!-- Show progress of the run. -->
<arg value="p"/>
<arg name="colors"/>


<file>src</file>
<file>tests/phpunit</file>
<file>tests/behat/bootstrap</file>
Expand All @@ -30,8 +33,9 @@
<exclude-pattern>*.Test\.php</exclude-pattern>
<exclude-pattern>*.TestCase\.php</exclude-pattern>
<exclude-pattern>*.test</exclude-pattern>
</rule> <!-- Allow missing function names in tests. -->
</rule>

<!-- Allow missing function names in tests. -->
<rule ref="Drupal.Commenting.DocComment.MissingShort">
<exclude-pattern>tests/behat/bootstrap/BehatCliTrait.php</exclude-pattern>
</rule>
Expand Down
8 changes: 5 additions & 3 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@
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([
__DIR__ . '/**',
]);

$rectorConfig->sets([
SetList::PHP_80,
SetList::PHP_81,
SetList::PHP_82,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::DEAD_CODE,
SetList::INSTANCEOF,
SetList::TYPE_DECLARATION,
]);

$rectorConfig->rule(DeclareStrictTypesRector::class);

$rectorConfig->skip([
// Rules added by Rector's rule sets.
CountArrayToEmptyArrayComparisonRector::class,
Expand All @@ -49,7 +51,7 @@
// Dependencies.
'*/vendor/*',
'*/node_modules/*',
'*/tests/*',
'tests/behat/bootstrap/BehatCliContext.php',
]);

$rectorConfig->fileExtensions([
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatScreenshotExtension\Context\Initializer;

use Behat\Behat\Context\Context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatScreenshotExtension\Context;

use Behat\Behat\Context\Context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatScreenshotExtension\Context;

use Behat\Behat\Hook\Scope\AfterStepScope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace DrevOps\BehatScreenshotExtension\ServiceContainer;

use Behat\Behat\Context\ServiceContainer\ContextExtension;
Expand Down
48 changes: 24 additions & 24 deletions tests/behat/bootstrap/BehatCliTrait.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
Expand All @@ -16,7 +18,7 @@ trait BehatCliTrait {
/**
* @BeforeScenario
*/
public function behatCliBeforeScenario(BeforeScenarioScope $scope) {
public function behatCliBeforeScenario(BeforeScenarioScope $scope): void {
if ($scope->getFeature()->hasTag('behatcli')) {
$traits = [
'tests/behat/bootstrap/ScreenshotTrait.php' => 'ScreenshotTrait',
Expand All @@ -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;
}
}

Expand All @@ -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}}' => '',
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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');
}

Expand All @@ -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);

Expand All @@ -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);

Expand Down
7 changes: 1 addition & 6 deletions tests/behat/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

/**
* @file
* Feature context Behat testing.
*/
declare(strict_types=1);

use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
Expand All @@ -18,8 +15,6 @@ class FeatureContext extends MinkContext implements Context {

/**
* Base URL for JavaScript scenarios.
*
* @var string
*/
protected string $javascriptBaseUrl;

Expand Down
5 changes: 1 addition & 4 deletions tests/behat/bootstrap/ScreenshotTrait.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?php

/**
* @file
* Feature context Behat testing.
*/
declare(strict_types=1);

/**
* Additional screenshot helpers.
Expand Down

0 comments on commit 02b10a1

Please sign in to comment.