Skip to content

Add PHPStan rules to forbid some PHP functions #19510

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"friendsoftwig/twigcs": "^6.4",
"glpi-project/tools": "^0.7",
"mikey179/vfsstream": "^1.6",
"nikic/php-parser": "^5.4",
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan": "^2.1",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ parameters:
message: '~/(downstream\.php|config_db\.php|config_db_slave\.php)" is not a file or it does not exist.~'
reportUnmatched: false
rules:
- Glpi\Tools\PHPStan\ForbidExitRule
- Glpi\Tools\PHPStan\ForbidHttpResponseCodeRule
- GlpiProject\Tools\PHPStan\Rules\GlobalVarTypeRule

# Copy and uncomment this content into a "phpstan.neon" file to add your own config values
Expand Down
69 changes: 69 additions & 0 deletions tools/src/PHPStan/ForbidExitRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Tools\PHPStan;

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

class ForbidExitRule implements Rule
{
public function getNodeType(): string
{
return Exit_::class;
}

public function processNode(Node $node, Scope $scope): array
{
$name = match ($node->getAttribute('kind')) {
Exit_::KIND_DIE => 'die',
Exit_::KIND_EXIT => 'exit',
default => 'die/exit',
};

return [
RuleErrorBuilder::message(
sprintf(
'You should not use the `%s` function. It prevents the execution of post-request/post-command routines.',
$name
)
)
->identifier('glpi.forbidExit')
->build()
];
}
}
70 changes: 70 additions & 0 deletions tools/src/PHPStan/ForbidHttpResponseCodeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Tools\PHPStan;

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

class ForbidHttpResponseCodeRule implements Rule
{
public function getNodeType(): string
{
return FuncCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (
$node instanceof FuncCall
&& $node->name instanceof Name
&& $node->name->toString() === 'http_response_code'
&& count($node->getRawArgs()) > 0 // `http_response_code()` used without args is a setter and does not cause issues
) {
return [
RuleErrorBuilder::message(
'You should not use the `http_response_code` function to change the response code. Due to a PHP bug, it may not provide the expected result (see https://bugs.php.net/bug.php?id=81451).',
)
->identifier('glpi.forbidHttpResponseCode')
->build()
];
}

return [];
}
}
Loading