Skip to content

Bump phpstan #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,29 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']
php-versions: ['8.2', '8.3']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
php-version: ${{ matrix.version }}
coverage: xdebug

- name: Install
run: make install

- name: Run linter
run: make lint analyse

- run: make install
- run: make lint
- run: make test

# - name: Run test & publish code coverage
# uses: paambaati/[email protected]
# env:
# CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
# with:
# coverageCommand: make test-coverage
# coverageLocations: ${{github.workplace}}/build/logs/clover.xml:clover
# debug: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/vendor/
composer.lock
*.cache
build
html-coverage
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ test:
composer exec -v phpunit tests

lint:
composer exec -v phpcs src tests
composer exec -v phpcs

analyse:
composer exec -v phpstan -- analyse -c phpstan.neon

lint-fix:
composer exec phpcbf -- --standard=PSR12 -v src tests
composer exec -v phpcbf

test-coverage:
composer exec --verbose phpunit tests -- --coverage-clover build/logs/clover.xml
XDEBUG_MODE=coverage composer exec --verbose phpunit tests -- --coverage-clover=build/logs/clover.xml

test-coverage-text:
XDEBUG_MODE=coverage composer exec --verbose phpunit tests -- --coverage-text

test-coverage-html:
XDEBUG_MODE=coverage composer exec --verbose phpunit tests -- --coverage-html=html-coverage
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "hexlet/phpstan-fp",
"description": "PHPStan rules for functional programming",
"version": "3.0.0",
"type": "library",
"require": {
"php": ">=8.1",
"phpstan/phpstan": "^1.10.24",
"illuminate/collections": "^10.14.1"
"php": ">=8.2",
"phpstan/phpstan": "^2.1",
"illuminate/collections": "^12"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Hexlet\\PHPStanFp\\": "src/",
"Hexlet\\PHPStanFp\\Tests\\": "tests/"
"Hexlet\\PHPStanFp\\": "src/",
"Hexlet\\PHPStanFp\\Tests\\": "tests/"
}
},
"extra": {
Expand All @@ -22,7 +23,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.5.18",
"squizlabs/php_codesniffer": "^3.6.2"
"phpunit/phpunit": "^11.5 | ^12.0",
"squizlabs/php_codesniffer": "^3.9"
}
}
7 changes: 5 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?xml version="1.0"?>
<ruleset name="ruleset">
<description>PSR12</description>

<arg name="basepath" value="."/>
<arg name="colors"/>
<arg name="parallel" value="100"/>
<arg value="np"/>
<arg value="n"/>
<arg value="p"/>

<file>src</file>
<file>tests</file>

<exclude-pattern>vendor/*</exclude-pattern>
<exclude-pattern>tests/*/data/*</exclude-pattern>
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ parameters:
paths:
- src
- tests

excludePaths:
- tests/*/data/*

checkGenericClassInNonGenericObjectType: false
reportUnmatchedIgnoredErrors: false
29 changes: 20 additions & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
backupGlobals="false"
backupStaticProperties="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnPhpunitDeprecations="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
21 changes: 15 additions & 6 deletions src/Rules/Classes/DisallowClassesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Hexlet\PHPStanFp\Rules\Classes;

use PHPStan\Rules\Rule;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\IdentifierRuleError;

class DisallowClassesRule implements Rule
{
Expand All @@ -22,15 +24,22 @@ public function getNodeType(): string
}

/**
* @param \PhpParser\Node\Stmt\Class_ $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[]
* @param Class_ $node
* @param Scope $scope
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$this->disallowClasses) {
return [];
}
return ['Should not use classes'];

$errorMessage = 'Should not use classes';

return [
RuleErrorBuilder::message($errorMessage)
->identifier('phpstanFunctionalProgramming.disallowClasses')
->build()
];
}
}
21 changes: 15 additions & 6 deletions src/Rules/Exceptions/DisallowThrowRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Hexlet\PHPStanFp\Rules\Exceptions;

use PHPStan\Rules\Rule;
use PhpParser\Node;
use PhpParser\Node\Expr\Throw_;
use PHPStan\Analyser\Scope;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\IdentifierRuleError;

class DisallowThrowRule implements Rule
{
Expand All @@ -22,15 +24,22 @@ public function getNodeType(): string
}

/**
* @param \PhpParser\Node\Stmt\Throw_ $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[]
* @param Throw_ $node
* @param Scope $scope
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$this->disallowThrow) {
return [];
}
return ['Should not use throw'];

$errorMessage = 'Should not use throw';

return [
RuleErrorBuilder::message($errorMessage)
->identifier('phpstanFunctionalProgramming.disallowThrow')
->build()
];
}
}
18 changes: 12 additions & 6 deletions src/Rules/Expression/DisallowUnusedExpressionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Hexlet\PHPStanFp\Rules\Expression;

use PHPStan\Rules\Rule;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\IdentifierRuleError;

class DisallowUnusedExpressionRule implements Rule
{
Expand All @@ -22,9 +24,9 @@ public function getNodeType(): string
}

/**
* @param \PhpParser\Node\Stmt\Expression $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[]
* @param Expression $node
* @param Scope $scope
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -36,6 +38,10 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return ['Enforce that an expression gets used'];
return [
RuleErrorBuilder::message('Enforce that an expression gets used')
->identifier('phpstanFunctionalProgramming.disallowUnusedExpression')
->build()
];
}
}
21 changes: 15 additions & 6 deletions src/Rules/Functions/DisallowMutatingFunctionsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Hexlet\PHPStanFp\Rules\Functions;

use PHPStan\Rules\Rule;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\IdentifierRuleError;

class DisallowMutatingFunctionsRule implements Rule
{
Expand Down Expand Up @@ -46,9 +48,9 @@ public function getNodeType(): string
}

/**
* @param \PhpParser\Node\Expr\FuncCall $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[]
* @param FuncCall $node
* @param Scope $scope
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -61,10 +63,17 @@ public function processNode(Node $node, Scope $scope): array
}

$name = $node->name->getFirst();

if (!in_array($name, $this->mutatingFunctionsNames)) {
return [];
}

return ["The use of function '{$name}' is not allowed as it might be a mutating function"];
$errorMessage = "The use of function '{$name}' is not allowed as it might be a mutating function";

return [
RuleErrorBuilder::message($errorMessage)
->identifier('phpstanFunctionalProgramming.disallowMutatingFunctions')
->build()
];
}
}
15 changes: 13 additions & 2 deletions src/Rules/Loops/DisallowLoopsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Hexlet\PHPStanFp\Rules\Loops;

use PHPStan\Rules\Rule;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\IdentifierRuleError;

abstract class DisallowLoopsRule implements Rule
{
Expand All @@ -15,13 +17,22 @@ public function __construct(bool $disallowLoops)
$this->disallowLoops = $disallowLoops;
}

/**
* @param Node $node
* @param Scope $scope
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$this->disallowLoops) {
return [];
}

return ["Should not use loop {$this->getLoopType()}"];
return [
RuleErrorBuilder::message("Should not use loop {$this->getLoopType()}")
->identifier('phpstanFunctionalProgramming.disallowLoops')
->build()
];
}

abstract protected function getLoopType(): string;
Expand Down
Loading