Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into chore/phpstan-setup
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jan 17, 2025
2 parents 402b751 + 70cf6bf commit df019bc
Show file tree
Hide file tree
Showing 41 changed files with 386 additions and 877 deletions.
15 changes: 15 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Git 2.23+ allows ignoring commits from git-blame, useful to not display large automated commits.
#
# This file is a list of such commits, that you most probably are not interested in.
# To make this work for your local setup, you need to run the following command:
#
# $ git config blame.ignoreRevsFile .git-blame-ignore-revs
#
# A few rules for changing this file:
# - Commits are in chronological order - oldest on top. Therefore new commits should be added in the end.
# - The purpose is specifically for large commits - avoid adding commits with a very small impact, even if related
# to automated code style formatting.
# - Each commit should be preceded with a comment detailing the purpose of the commit.

# PHP-CS-Fixer Setup - Apply automated changes
cc23fdc9376975d209489ae88b22ed945871d9cb
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
/CONTRIBUTING.md export-ignore
/phpunit.dist.xml export-ignore
/.php-cs-fixer.dist.php export-ignore
/.git-blame-ignore-revs export-ignore
/phpstan.dist.neon export-ignore
/phpstan-baseline.neon export-ignore
6 changes: 3 additions & 3 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

return (new PhpCsFixer\Config())
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setRiskyAllowed(true)
->setRules([
'@PER-CS' => true,
'@Symfony' => true,
Expand All @@ -28,7 +27,8 @@
'phpdoc_align' => ['align' => 'left'],
'heredoc_to_nowdoc' => true,
'heredoc_indentation' => ['indentation' => 'same_as_start'],
'phpdoc_array_type' => true,
'phpdoc_list_type' => true,
'single_line_throw' => false,
'ternary_to_null_coalescing' => true,
'global_namespace_import' => false,
])
->setFinder($finder);
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
],

"require": {
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*"
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*",
"composer-runtime-api": "^2.2"
},

"require-dev": {
"symfony/filesystem": "^5.4 || ^6.4 || ^7.0",
"symfony/yaml": "^5.4 || ^6.4 || ^7.0",
"phpunit/phpunit": "^10.5",
"cucumber/cucumber": "dev-gherkin-24.1.0",
Expand Down Expand Up @@ -73,7 +75,7 @@
"lint": [
"Composer\\Config::disableProcessTimeout",
"vendor/bin/phpstan analyse --no-progress --memory-limit 512M",
"vendor/bin/php-cs-fixer fix --dry-run --diff --show-progress=dots"
"vendor/bin/php-cs-fixer check --diff --show-progress=dots --verbose"
],
"test": [
"Composer\\Config::disableProcessTimeout",
Expand Down
15 changes: 13 additions & 2 deletions src/Behat/Gherkin/Cache/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
namespace Behat\Gherkin\Cache;

use Behat\Gherkin\Exception\CacheException;
use Behat\Gherkin\Gherkin;
use Behat\Gherkin\Node\FeatureNode;
use Composer\InstalledVersions;

/**
* File cache.
Expand All @@ -24,6 +24,17 @@ class FileCache implements CacheInterface
{
private $path;

/**
* Used as part of the cache directory path to invalidate cache if the installed package version changes.
*/
private static function getGherkinVersionHash(): string
{
$version = InstalledVersions::getVersion('behat/gherkin');

// Composer version strings can contain arbitrary content so hash for filesystem safety
return md5($version);
}

/**
* Initializes file cache.
*
Expand All @@ -33,7 +44,7 @@ class FileCache implements CacheInterface
*/
public function __construct($path)
{
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'v' . Gherkin::VERSION;
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . self::getGherkinVersionHash();

if (!is_dir($this->path)) {
@mkdir($this->path, 0777, true);
Expand Down
6 changes: 3 additions & 3 deletions src/Behat/Gherkin/Filter/LineRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class LineRangeFilter implements FilterInterface
*/
public function __construct($filterMinLine, $filterMaxLine)
{
$this->filterMinLine = intval($filterMinLine);
if ($filterMaxLine == '*') {
$this->filterMinLine = (int) $filterMinLine;
if ($filterMaxLine === '*') {
$this->filterMaxLine = PHP_INT_MAX;
} else {
$this->filterMaxLine = intval($filterMaxLine);
$this->filterMaxLine = (int) $filterMaxLine;
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/Behat/Gherkin/Filter/NameFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public function isFeatureMatch(FeatureNode $feature)
}

if ($this->filterString[0] === '/') {
return preg_match($this->filterString, $feature->getTitle()) === 1;
return (bool) preg_match($this->filterString, $feature->getTitle());
}

return mb_strpos($feature->getTitle(), $this->filterString, 0, 'utf8') !== false;
return str_contains($feature->getTitle(), $this->filterString);
}

/**
Expand All @@ -65,9 +65,11 @@ public function isScenarioMatch(ScenarioInterface $scenario)
return false;
}

if ($this->filterString[0] === '/' && preg_match($this->filterString, $scenario->getTitle()) === 1) {
if ($this->filterString[0] === '/' && preg_match($this->filterString, $scenario->getTitle())) {
return true;
} elseif (mb_strpos($scenario->getTitle(), $this->filterString, 0, 'utf8') !== false) {
}

if (str_contains($scenario->getTitle(), $this->filterString)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Behat/Gherkin/Filter/NarrativeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct($regex)
*/
public function isFeatureMatch(FeatureNode $feature)
{
return preg_match($this->regex, $feature->getDescription() ?? '') === 1;
return (bool) preg_match($this->regex, $feature->getDescription() ?? '');
}

/**
Expand Down
20 changes: 13 additions & 7 deletions src/Behat/Gherkin/Filter/RoleFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ class RoleFilter extends SimpleFilter
*/
public function __construct($role)
{
$this->pattern = '/as an? ' . strtr(preg_quote($role, '/'), [
'\*' => '.*',
'\?' => '.',
'\[' => '[',
'\]' => ']',
]) . '[$\n]/i';
$this->pattern = sprintf(
'/as an? %s[$\n]/i',
strtr(
preg_quote($role, '/'),
[
'\*' => '.*',
'\?' => '.',
'\[' => '[',
'\]' => ']',
]
)
);
}

/**
Expand All @@ -46,7 +52,7 @@ public function __construct($role)
*/
public function isFeatureMatch(FeatureNode $feature)
{
return preg_match($this->pattern, $feature->getDescription() ?? '') === 1;
return (bool) preg_match($this->pattern, $feature->getDescription() ?? '');
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Behat/Gherkin/Filter/TagFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Behat\Gherkin\Node\OutlineNode;
use Behat\Gherkin\Node\ScenarioInterface;

use function strlen;

/**
* Filters scenarios by feature/scenario tag.
*
Expand Down Expand Up @@ -134,7 +136,7 @@ protected function isTagsMatchCondition($tags)
{
$satisfies = true;

if (\strlen($this->filterString) === 0) {
if (strlen($this->filterString) === 0) {
return $satisfies;
}

Expand Down
5 changes: 5 additions & 0 deletions src/Behat/Gherkin/Gherkin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
class Gherkin
{
/**
* @deprecated this constant will not be updated for releases after 4.8.0 and will be removed in the next major.
* You can use composer's runtime API to get the behat version if you need it. Note that composer's versions will
* not always be simple numeric values.
*/
public const VERSION = '4.8.0';

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Behat/Gherkin/Keywords/CucumberKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function getButKeywords()
*/
private function prepareStepString($keywordsString)
{
if (mb_strpos($keywordsString, '*|', 0, 'UTF-8') === 0) {
if (str_starts_with($keywordsString, '*|')) {
$keywordsString = mb_substr($keywordsString, 2, mb_strlen($keywordsString, 'utf8') - 2, 'utf8');
}

Expand Down
6 changes: 4 additions & 2 deletions src/Behat/Gherkin/Keywords/KeywordsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public function setKeywordsDumperFunction($mapper)
public function dumpKeywords(array $keywords, $isShort)
{
if ($isShort) {
return count($keywords) > 1 ? '(' . implode('|', $keywords) . ')' : $keywords[0];
return count($keywords) > 1
? '(' . implode('|', $keywords) . ')'
: $keywords[0];
}

return $keywords[0];
Expand Down Expand Up @@ -348,7 +350,7 @@ function ($keyword) {
}

$indent = ' ';
if (mb_strpos($keyword, '<', 0, 'utf8') !== false) {
if (str_contains($keyword, '<')) {
$keyword = mb_substr($keyword, 0, -1, 'utf8');
$indent = '';
}
Expand Down
22 changes: 9 additions & 13 deletions src/Behat/Gherkin/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@ public function deferToken(array $token)
*/
public function predictToken()
{
if ($this->stashedToken === null) {
$this->stashedToken = $this->getNextToken();
}

return $this->stashedToken;
return $this->stashedToken ??= $this->getNextToken();
}

/**
Expand Down Expand Up @@ -196,7 +192,7 @@ protected function consumeLineUntil(int $trimmedOffset)
*/
protected function getTrimmedLine()
{
return $this->trimmedLine !== null ? $this->trimmedLine : $this->trimmedLine = trim($this->line);
return $this->trimmedLine ??= trim($this->line);
}

/**
Expand Down Expand Up @@ -343,7 +339,7 @@ protected function getKeywords($type)
if ($type === 'Step') {
$padded = [];
foreach (explode('|', $keywords) as $keyword) {
$padded[] = mb_strpos($keyword, '<', 0, 'utf8') !== false
$padded[] = str_contains($keyword, '<')
? preg_quote(mb_substr($keyword, 0, -1, 'utf8'), '/') . '\s*'
: preg_quote($keyword, '/') . '\s+';
}
Expand Down Expand Up @@ -499,7 +495,7 @@ protected function scanTableRow()
}

$line = $this->getTrimmedLine();
if (!isset($line[0]) || $line[0] !== '|' || substr($line, -1) !== '|') {
if ($line === '' || !str_starts_with($line, '|') || !str_ends_with($line, '|')) {
return null;
}

Expand All @@ -524,7 +520,7 @@ protected function scanTags()
{
$line = $this->getTrimmedLine();

if (!isset($line[0]) || $line[0] !== '@') {
if ($line === '' || !str_starts_with($line, '@')) {
return null;
}

Expand Down Expand Up @@ -558,11 +554,11 @@ protected function scanLanguage()
return null;
}

if (mb_strpos(ltrim($this->line), '#', 0, 'utf8') !== 0) {
if (!str_starts_with(ltrim($this->line), '#')) {
return null;
}

return $this->scanInput('/^\s*\#\s*language:\s*([\w_\-]+)\s*$/', 'Language');
return $this->scanInput('/^\s*#\s*language:\s*([\w_\-]+)\s*$/', 'Language');
}

/**
Expand All @@ -577,7 +573,7 @@ protected function scanComment()
}

$line = $this->getTrimmedLine();
if (mb_strpos($line, '#', 0, 'utf8') !== 0) {
if (!str_starts_with($line, '#')) {
return null;
}

Expand Down Expand Up @@ -642,7 +638,7 @@ private function getStepKeywordType($native)
}

foreach ($this->stepKeywordTypesCache as $type => $keywords) {
if (in_array($native, $keywords) || in_array($native . '<', $keywords)) {
if (in_array($native, $keywords, true) || in_array($native . '<', $keywords, true)) {
return $type;
}
}
Expand Down
Loading

0 comments on commit df019bc

Please sign in to comment.