mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-18 15:05:56 +00:00
phpstan: added rule to ban new $class
see #6635 for rationale on why we want to get rid of this for now, this rule will prevent this anti-feature from being used in new code
This commit is contained in:
parent
09f0ce458c
commit
2291546610
@ -11,6 +11,7 @@ includes:
|
|||||||
|
|
||||||
rules:
|
rules:
|
||||||
- pocketmine\phpstan\rules\DeprecatedLegacyEnumAccessRule
|
- pocketmine\phpstan\rules\DeprecatedLegacyEnumAccessRule
|
||||||
|
- pocketmine\phpstan\rules\DisallowDynamicNewRule
|
||||||
- pocketmine\phpstan\rules\DisallowEnumComparisonRule
|
- pocketmine\phpstan\rules\DisallowEnumComparisonRule
|
||||||
- pocketmine\phpstan\rules\DisallowForeachByReferenceRule
|
- pocketmine\phpstan\rules\DisallowForeachByReferenceRule
|
||||||
- pocketmine\phpstan\rules\ExplodeLimitRule
|
- pocketmine\phpstan\rules\ExplodeLimitRule
|
||||||
|
@ -18,6 +18,12 @@ parameters:
|
|||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/Server.php
|
path: ../../../src/Server.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: '#^Dynamic new is not allowed\.$#'
|
||||||
|
identifier: pocketmine.new.noDynamic
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/Server.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: '#^Method pocketmine\\Server\:\:getCommandAliases\(\) should return array\<string, list\<string\>\> but returns array\<string, array\<mixed\>\>\.$#'
|
message: '#^Method pocketmine\\Server\:\:getCommandAliases\(\) should return array\<string, list\<string\>\> but returns array\<string, array\<mixed\>\>\.$#'
|
||||||
identifier: return.type
|
identifier: return.type
|
||||||
@ -54,6 +60,12 @@ parameters:
|
|||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/VersionInfo.php
|
path: ../../../src/VersionInfo.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: '#^Dynamic new is not allowed\.$#'
|
||||||
|
identifier: pocketmine.new.noDynamic
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/block/Block.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: '#^Parameter \#1 \$x of method pocketmine\\world\\World\:\:getBlockAt\(\) expects int, float\|int given\.$#'
|
message: '#^Parameter \#1 \$x of method pocketmine\\world\\World\:\:getBlockAt\(\) expects int, float\|int given\.$#'
|
||||||
identifier: argument.type
|
identifier: argument.type
|
||||||
@ -510,6 +522,12 @@ parameters:
|
|||||||
count: 3
|
count: 3
|
||||||
path: ../../../src/block/tile/Spawnable.php
|
path: ../../../src/block/tile/Spawnable.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: '#^Dynamic new is not allowed\.$#'
|
||||||
|
identifier: pocketmine.new.noDynamic
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/block/tile/TileFactory.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: '#^Parameter \#1 \$x of method pocketmine\\world\\World\:\:getPotentialLightAt\(\) expects int, float\|int given\.$#'
|
message: '#^Parameter \#1 \$x of method pocketmine\\world\\World\:\:getPotentialLightAt\(\) expects int, float\|int given\.$#'
|
||||||
identifier: argument.type
|
identifier: argument.type
|
||||||
@ -936,6 +954,12 @@ parameters:
|
|||||||
count: 4
|
count: 4
|
||||||
path: ../../../src/plugin/PluginManager.php
|
path: ../../../src/plugin/PluginManager.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: '#^Dynamic new is not allowed\.$#'
|
||||||
|
identifier: pocketmine.new.noDynamic
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/plugin/PluginManager.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: '#^Method pocketmine\\resourcepacks\\ZippedResourcePack\:\:getPackSize\(\) should return int but returns int\<0, max\>\|false\.$#'
|
message: '#^Method pocketmine\\resourcepacks\\ZippedResourcePack\:\:getPackSize\(\) should return int but returns int\<0, max\>\|false\.$#'
|
||||||
identifier: return.type
|
identifier: return.type
|
||||||
@ -1248,6 +1272,12 @@ parameters:
|
|||||||
count: 1
|
count: 1
|
||||||
path: ../../../src/world/format/io/region/RegionLoader.php
|
path: ../../../src/world/format/io/region/RegionLoader.php
|
||||||
|
|
||||||
|
-
|
||||||
|
message: '#^Dynamic new is not allowed\.$#'
|
||||||
|
identifier: pocketmine.new.noDynamic
|
||||||
|
count: 1
|
||||||
|
path: ../../../src/world/generator/GeneratorRegisterTask.php
|
||||||
|
|
||||||
-
|
-
|
||||||
message: '#^Method pocketmine\\world\\generator\\biome\\BiomeSelector\:\:pickBiome\(\) should return pocketmine\\world\\biome\\Biome but returns pocketmine\\world\\biome\\Biome\|null\.$#'
|
message: '#^Method pocketmine\\world\\generator\\biome\\BiomeSelector\:\:pickBiome\(\) should return pocketmine\\world\\biome\\Biome but returns pocketmine\\world\\biome\\Biome\|null\.$#'
|
||||||
identifier: return.type
|
identifier: return.type
|
||||||
|
55
tests/phpstan/rules/DisallowDynamicNewRule.php
Normal file
55
tests/phpstan/rules/DisallowDynamicNewRule.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace pocketmine\phpstan\rules;
|
||||||
|
|
||||||
|
use PhpParser\Node;
|
||||||
|
use PhpParser\Node\Expr;
|
||||||
|
use PhpParser\Node\Expr\New_;
|
||||||
|
use PHPStan\Analyser\Scope;
|
||||||
|
use PHPStan\Rules\Rule;
|
||||||
|
use PHPStan\Rules\RuleErrorBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @phpstan-implements Rule<New_>
|
||||||
|
*/
|
||||||
|
final class DisallowDynamicNewRule implements Rule{
|
||||||
|
|
||||||
|
public function getNodeType() : string{
|
||||||
|
return New_::class;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function processNode(Node $node, Scope $scope) : array{
|
||||||
|
/** @var New_ $node */
|
||||||
|
if($node->class instanceof Expr){
|
||||||
|
return [
|
||||||
|
RuleErrorBuilder::message("Dynamic new is not allowed.")
|
||||||
|
->tip("For factories, use closures instead. Closures can implement custom logic, are statically analyzable, and don't restrict the constructor signature.")
|
||||||
|
->identifier("pocketmine.new.noDynamic")
|
||||||
|
->build()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user