Giter VIP home page Giter VIP logo

phpstan-rules's People

Contributors

aleswita avatar dependabot[bot] avatar enumag avatar janedbal avatar jantvrdik avatar martinmystikjonas avatar milanpala avatar olsavmic avatar voku avatar xificurk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

phpstan-rules's Issues

"ForbidPhpDocNullabilityMismatchWithNativeTypehintRule" with @template is not working as expected

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;
    }

immediatelyCalledCallables does not work for manually invoked anonymous functions

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));

EnforceReadonlyPublicPropertyRule false positive with property override

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;
}
PHPStan output: 1/1 [โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“โ–“] 100%

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


Upgrade to PHP-Parser 5

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.

ForbidEnumInFunctionArgumentsRule shouldn't report error when SORT_REGULAR is used as second argument

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.

AllowNamedArgumentOnlyInAttributesRule - issue with nested attributes

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?

Current release broken ?

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!

forbidUnsetClassField disable is not respected

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

allowComparingOnlyComparableTypes bool <=> bool

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.