Skip to content

Commit

Permalink
Merge pull request #276 from uuf6429/chore/phpstan-setup
Browse files Browse the repository at this point in the history
PHPStan setup & level1 fixes
  • Loading branch information
carlos-granados authored Jan 31, 2025
2 parents 70cf6bf + 6bbb4d3 commit bd74622
Show file tree
Hide file tree
Showing 24 changed files with 212 additions and 232 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/phpunit.dist.xml export-ignore
/.php-cs-fixer.dist.php export-ignore
/.git-blame-ignore-revs export-ignore
/phpstan.dist.neon export-ignore
21 changes: 14 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ on:
types: [ created ]

jobs:
check_code_style:
name: Check code style
check_composer:
name: Check composer.json
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: '8.3'
- run: composer validate --strict --no-check-lock

- name: Install dependencies
run: composer update

- run: ./vendor/bin/php-cs-fixer fix --dry-run --diff --show-progress=dots
check_code:
name: Check code style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: '8.3'
- run: composer update
- run: composer lint

tests:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ composer.lock
phpunit.xml
.php-cs-fixer.php
.php-cs-fixer.cache
phpstan.neon
2 changes: 1 addition & 1 deletion bin/update_cucumber
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ foreach (json_decode($composerConfig, true, 512, JSON_THROW_ON_ERROR)['repositor
}
}

if (!isset($oldHash)) {
if (!isset($oldHash, $oldTag)) {
echo "ERROR: Could not parse the composer configuration\n";
exit(1);
}
Expand Down
18 changes: 13 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"symfony/yaml": "^5.4 || ^6.4 || ^7.0",
"phpunit/phpunit": "^10.5",
"cucumber/cucumber": "dev-gherkin-24.1.0",
"friendsofphp/php-cs-fixer": "^3.65"
"friendsofphp/php-cs-fixer": "^3.65",
"phpstan/phpstan": "^2",
"phpstan/extension-installer": "^1",
"phpstan/phpstan-phpunit": "^2"
},

"suggest": {
Expand Down Expand Up @@ -71,20 +74,25 @@
"scripts": {
"lint": [
"Composer\\Config::disableProcessTimeout",
"vendor/bin/php-cs-fixer check --diff --show-progress=dots --verbose"
"vendor/bin/phpstan analyze --ansi --no-progress --memory-limit 512M",
"vendor/bin/phpstan analyze bin/cucumber_changelog --ansi --no-progress --memory-limit 512M",
"vendor/bin/phpstan analyze bin/update_cucumber --ansi --no-progress --memory-limit 512M",
"vendor/bin/phpstan analyze bin/update_i18n --ansi --no-progress --memory-limit 512M",
"vendor/bin/php-cs-fixer check --diff --ansi --show-progress=dots --verbose"
],
"test": [
"Composer\\Config::disableProcessTimeout",
"vendor/bin/phpunit ./tests"
"vendor/bin/phpunit --colors"
],
"fix": [
"Composer\\Config::disableProcessTimeout",
"vendor/bin/php-cs-fixer fix --diff --show-progress=dots"
"vendor/bin/php-cs-fixer fix --diff --ansi --show-progress=dots"
]
},

"config": {
"allow-plugins": {
"phpstan/extension-installer": false
"phpstan/extension-installer": true
}
}
}
5 changes: 5 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 1
paths:
- src
- tests
20 changes: 9 additions & 11 deletions src/Behat/Gherkin/Filter/PathsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ class PathsFilter extends SimpleFilter
/**
* Initializes filter.
*
* @param string[] $paths List of approved paths
* @param array<array-key, string> $paths List of approved paths
*/
public function __construct(array $paths)
{
$this->filterPaths = array_map(
function ($realpath) {
return rtrim($realpath, DIRECTORY_SEPARATOR) .
(is_dir($realpath) ? DIRECTORY_SEPARATOR : '');
},
array_filter(
array_map('realpath', $paths)
)
);
foreach ($paths as $path) {
if (($realpath = realpath($path)) === false) {
continue;
}
$this->filterPaths[] = rtrim($realpath, DIRECTORY_SEPARATOR)
. (is_dir($realpath) ? DIRECTORY_SEPARATOR : '');
}
}

/**
Expand All @@ -50,7 +48,7 @@ function ($realpath) {
public function isFeatureMatch(FeatureNode $feature)
{
foreach ($this->filterPaths as $path) {
if (strpos(realpath($feature->getFile()), $path) === 0) {
if (str_starts_with(realpath($feature->getFile()), $path)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Behat/Gherkin/Filter/TagFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function isScenarioMatch(FeatureNode $feature, ScenarioInterface $scenari
/**
* Checks that node matches condition.
*
* @param string[] $tags
* @param array<array-key, string> $tags
*
* @return bool
*/
Expand Down
16 changes: 8 additions & 8 deletions src/Behat/Gherkin/Gherkin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class Gherkin
public const VERSION = '4.8.0';

/**
* @var LoaderInterface[]
* @var list<LoaderInterface>
*/
protected $loaders = [];
/**
* @var FeatureFilterInterface[]
* @var list<FeatureFilterInterface>
*/
protected $filters = [];

Expand All @@ -62,12 +62,12 @@ public function addFilter(FeatureFilterInterface $filter)
/**
* Sets filters to the parser.
*
* @param FeatureFilterInterface[] $filters
* @param array<array-key, FeatureFilterInterface> $filters
*/
public function setFilters(array $filters)
{
$this->filters = [];
array_map([$this, 'addFilter'], $filters);
array_map($this->addFilter(...), $filters);
}

/**
Expand All @@ -88,7 +88,7 @@ public function setBasePath($path)
* Loads & filters resource with added loaders.
*
* @param mixed $resource Resource to load
* @param FeatureFilterInterface[] $filters Additional filters
* @param array<array-key, FeatureFilterInterface> $filters Additional filters
*
* @return array
*/
Expand All @@ -97,10 +97,10 @@ public function load($resource, array $filters = [])
$filters = array_merge($this->filters, $filters);

$matches = [];
if (preg_match('/^(.*)\:(\d+)-(\d+|\*)$/', $resource, $matches)) {
if (preg_match('/^(.*):(\d+)-(\d+|\*)$/', $resource, $matches)) {
$resource = $matches[1];
$filters[] = new LineRangeFilter($matches[2], $matches[3]);
} elseif (preg_match('/^(.*)\:(\d+)$/', $resource, $matches)) {
} elseif (preg_match('/^(.*):(\d+)$/', $resource, $matches)) {
$resource = $matches[1];
$filters[] = new LineFilter($matches[2]);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public function load($resource, array $filters = [])
*
* @param mixed $resource Resource to load
*
* @return LoaderInterface
* @return LoaderInterface|null
*/
public function resolveLoader($resource)
{
Expand Down
20 changes: 10 additions & 10 deletions src/Behat/Gherkin/Keywords/ArrayKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function setLanguage($language)
}

/**
* Returns Feature keywords (splitted by "|").
* Returns Feature keywords (separated by "|").
*
* @return string
*/
Expand All @@ -83,7 +83,7 @@ public function getFeatureKeywords()
}

/**
* Returns Background keywords (splitted by "|").
* Returns Background keywords (separated by "|").
*
* @return string
*/
Expand All @@ -93,7 +93,7 @@ public function getBackgroundKeywords()
}

/**
* Returns Scenario keywords (splitted by "|").
* Returns Scenario keywords (separated by "|").
*
* @return string
*/
Expand All @@ -103,7 +103,7 @@ public function getScenarioKeywords()
}

/**
* Returns Scenario Outline keywords (splitted by "|").
* Returns Scenario Outline keywords (separated by "|").
*
* @return string
*/
Expand All @@ -113,7 +113,7 @@ public function getOutlineKeywords()
}

/**
* Returns Examples keywords (splitted by "|").
* Returns Examples keywords (separated by "|").
*
* @return string
*/
Expand All @@ -123,7 +123,7 @@ public function getExamplesKeywords()
}

/**
* Returns Given keywords (splitted by "|").
* Returns Given keywords (separated by "|").
*
* @return string
*/
Expand All @@ -133,7 +133,7 @@ public function getGivenKeywords()
}

/**
* Returns When keywords (splitted by "|").
* Returns When keywords (separated by "|").
*
* @return string
*/
Expand All @@ -143,7 +143,7 @@ public function getWhenKeywords()
}

/**
* Returns Then keywords (splitted by "|").
* Returns Then keywords (separated by "|").
*
* @return string
*/
Expand All @@ -153,7 +153,7 @@ public function getThenKeywords()
}

/**
* Returns And keywords (splitted by "|").
* Returns And keywords (separated by "|").
*
* @return string
*/
Expand All @@ -163,7 +163,7 @@ public function getAndKeywords()
}

/**
* Returns But keywords (splitted by "|").
* Returns But keywords (separated by "|").
*
* @return string
*/
Expand Down
14 changes: 7 additions & 7 deletions src/Behat/Gherkin/Keywords/CucumberKeywords.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function __construct($yaml)
{
// Handle filename explicitly for BC reasons, as Symfony Yaml 3.0 does not do it anymore
$file = null;
if (strpos($yaml, "\n") === false && is_file($yaml)) {
if (is_readable($yaml) === false) {
if (!str_contains($yaml, "\n") && is_file($yaml)) {
if (!is_readable($yaml)) {
throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $yaml));
}

Expand All @@ -54,7 +54,7 @@ public function __construct($yaml)
}

/**
* Returns Feature keywords (splitted by "|").
* Returns Feature keywords (separated by "|").
*
* @return string
*/
Expand All @@ -64,7 +64,7 @@ public function getGivenKeywords()
}

/**
* Returns When keywords (splitted by "|").
* Returns When keywords (separated by "|").
*
* @return string
*/
Expand All @@ -74,7 +74,7 @@ public function getWhenKeywords()
}

/**
* Returns Then keywords (splitted by "|").
* Returns Then keywords (separated by "|").
*
* @return string
*/
Expand All @@ -84,7 +84,7 @@ public function getThenKeywords()
}

/**
* Returns And keywords (splitted by "|").
* Returns And keywords (separated by "|").
*
* @return string
*/
Expand All @@ -94,7 +94,7 @@ public function getAndKeywords()
}

/**
* Returns But keywords (splitted by "|").
* Returns But keywords (separated by "|").
*
* @return string
*/
Expand Down
Loading

0 comments on commit bd74622

Please sign in to comment.