shipmonk-rnd / phpstan-rules Goto Github PK
View Code? Open in Web Editor NEW๐ Various extra strict PHPStan rules we found useful in ShipMonk.
๐ Various extra strict PHPStan rules we found useful in ShipMonk.
Recent phpunit/phpunit:^11, released on February 2, 2024, requires nikic/php-parser ^5.0.
However, phpstan-rules:2.11.12
requires nikic/php-parser": "^4.14.0
.
Upgrading PHPUnit is therefore blocked by phpstan-rules.
PHPUnit looks to me a popular quality tool to run alongside PHPStan, therefore phpstan-rules should consider updating.
There is not much we can do about this as this is a limitation of PHPStan but let's document the issue for others.
Since PHPStan does not provide FunctionReflection
for anonymous functions, function throwTypes
are not easily extractable, nor DynamicFunctionThrowTypeExtension
/ DynamicFunctionReturnTypeExtension
(eg. ImmediatelyCalledCallableThrowTypeExtension) are applied.
This means that the immediatelyCalledCallables
rule does not work correctly when an anonymous function is invoked manually:
$result = (static fn ($val) => throw new Exception($val))(); // No @throws reported here as the throw type extension is not invoked.
This pattern is especially useful in cases like:
return match ($format) {
ShippingLabelFormat::Zpl => ...,
ShippingLabelFormat::Pdf => (function () use ($format): LabelResult {
try {
...
} catch (SomeFailure $e) {
throw $e;
}
})(),
};
As opposed to following (which works just fine):
// works correctly as the analysis is performed on the outer (array_map) function with the anonymous function as parameter.
array_map(static fn ($val) => throw new Exception($val));
Hello
Read-only properties are only available with PHP 8.1+
So EnforceReadonlyPublicPropertyRule
reports false-positive with public properties with PHP 7.4
Would you accept a PR to fix this?
e.g.: "The @return phpdoc does not contain null, but native return type does"
/**
* @param string $name
* @param null|string $fallback
*
* @template TServerGetStringFallback as string|null
* @phpstan-param TServerGetStringFallback $fallback
* @phpstan-return string|TServerGetStringFallback
*/
public static function getString($name, $fallback = null): ?string {
// @codingStandardsIgnoreStart
if (isset($_SERVER[$name])) {
$value = $_SERVER[$name];
return $value;
}
// @codingStandardsIgnoreEnd
return $fallback;
}
I like ForbidAssignmentNotMatchingVarDocRule rule but in our codebase we often use @var comments to specify narrower type not properly detected by PHPStan. Would you welcome PR with configuration option that would allow type narrowing for this rule?
Argument 1 in array_unique() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
For this code
$countries = [Country::CZ, Country::CZ, Country::SK];
array_unique($countries, SORT_REGULAR)
shouldn't be reported this error, because of second argument the __toString is not used for compare.
Probably there are some other combinations of other functions which check this rule where there are different default $flag for comparing method type.
After merging phpstan/phpstan-src#2778 your rule ForbidUnusedMatchResultRule fails with false positives.
I think it can be fixed by using $scope->getKeepVoidType()
instead of $scope->getType()
.
Consider this code:
#[OA\Response(
response: '200',
description: 'Lorem ipsum',
content: new OA\JsonContent(
ref: new Model(
type: CustomResponseClass::class
)
)
)]
Both the ref:
and type:
trigger "Named arguments are allowed only within native attributes" because here JsonContent and Model are not actually considered attributes as they're created using new
. Personally though in this context I'd like to allow named arguments as they're nested attributes.
What do you think?
Hello
Thank you for this great set of phpstan rules.
We developed http://github.com/assoconnect/php-date which supports comparison.
allowComparingOnlyComparableTypes
is powerful but isn't customizable so I can't enable it.
Could you please make this rule accept a whitelist of function which supports comparison?
Thank you!
Hi there!
In case of property override that rule fails, but PHP doesn't allow to add readonly
modifier to overriden property.
42 Public property `timestamps` not marked as readonly.
โ๏ธ Entities/Foo.php
It happened with Laravel Eloquent Model: https://github.com/laravel/framework/blob/ef76b3a4dc0616960a1532c29cad3b8c429f8597/src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php#L14C31-L14C31
Simple code example:
<?php
declare(strict_types=1);
namespace B2B\TCA;
abstract class VendorClass
{
public $property = true;
}
final class User extends VendorClass
{
public $property = false;
}
Line Foo.php
3 Multiple class/interface/trait is not allowed in single file
โ๏ธ Foo.php
9 Property B2B\TCA\VendorClass::$property has no type specified.
โ๏ธ Foo.php
9 Public property property
not marked as readonly.
โ๏ธ Foo.php
14 Property B2B\TCA\User::$property has no type specified.
โ๏ธ Foo.php
14 Public property property
not marked as readonly.
โ๏ธ Foo.php
The rule reports false positive for construct:
$string .= match (/* ... */) {/* ... */};
I skiped the installation of "nikic/php-parser" and it's still working because phpstan/phpstan (the phar file) has his dependency already included, or?
same as here: sidz/phpstan-rules#25
I need to remove both ForbidMethodCallOnMixedRule
and ForbidFetchOnMixedRule
from neon.rules
in order to make it work.
I think I already stumbled upon a commit that solves this.
JFYI :)
Thank you & great work!
I think there is no problem with bool <=> bool
in php leads to report this error:
Comparison bool <=> bool contains non-comparable type, only int|float|string|DateTimeInterface or comparable tuple is allowed.
In php this works correctly and I cannot see any reason to report this as error.
true <=> true is 0
true <=> false is 1
false <=> true is -1
false <=> false is 0
It is possible to change the rule to allow this combination? Thank you.
Hello
seems like the rule ForbidUnsetClassFieldRule
is registered twice. When I unset some property from a class, it's reported twice. When I disable the rule by
------ ----------------------------------------------------------------------------------------
Line ApiModule/Model/XXX.php
------ ----------------------------------------------------------------------------------------
869 Unsetting class field is forbidden as it causes un-initialization, assign null instead
869 Unsetting class field is forbidden as it causes un-initialization, assign null instead
870 Unsetting class field is forbidden as it causes un-initialization, assign null instead
870 Unsetting class field is forbidden as it causes un-initialization, assign null instead
------ ----------------------------------------------------------------------------------------
forbidUnsetClassField:
enabled: false
------ ----------------------------------------------------------------------------------------
Line ApiModule/Model/XXX.php
------ ----------------------------------------------------------------------------------------
869 Unsetting class field is forbidden as it causes un-initialization, assign null instead
870 Unsetting class field is forbidden as it causes un-initialization, assign null instead
------ ----------------------------------------------------------------------------------------
its reported once.
Thanks
Jakub
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.