Skip to content
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

PHPStan throws violation on accessing ->because() after ->excluding() #238

Closed
dkreuer opened this issue Oct 5, 2023 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@dkreuer
Copy link

dkreuer commented Oct 5, 2023

Bug Description
Running PHPat as documented while validating test files with PHPStan throws violation, accessing ->because() after ->excluding() because ->excluding() returns a TipOrBuildStep but is annotated to return a BuildStep, making calling ->because() polymorphic and therefore invalid.

Additional context

<?php
// tests/Module/Iam/Architecture/DisallowModuleAccess.php

declare(strict_types=1);

namespace App\Module\Iam\Tests\Architecture;

use PHPat\Selector\Selector;
use PHPat\Test\Builder\Rule;
use PHPat\Test\PHPat;

final class DisallowModuleAccess
{
    public function test_module_only_accesses_other_modules_public_api(): Rule
    {
        return PHPat::rule()
            ->classes(Selector::inNamespace('App\\Module\\Iam\\Application'))
            ->shouldNotDependOn()
                ->classes(
                    Selector::inNamespace('App\\Module'),
                )
                ->excluding(
                    Selector::inNamespace('App\\Module\\Iam'),
                    Selector::inNamespace('/^App\\\\Module\\\\(.*?)\\\\Presentation\\\\Php/', true),
                )
                ->because('modules must not depend on other modules internals');
    }
}

results in

 ------ ---------------------------------------------------------------------- 
  Line   tests/Module/Iam/Architecture/DisallowModuleAccess.php                
 ------ ---------------------------------------------------------------------- 
  15     Call to an undefined method PHPat\Test\Builder\BuildStep::because().  
 ------ ---------------------------------------------------------------------- 

Proposed solution
By changing the signature of ->excluding() to return TipOrBuildStep the polymorphic call goes away. I'm happy to provide a PR for that change.

@dkreuer dkreuer added the bug Something isn't working label Oct 5, 2023
@carlosas
Copy link
Owner

carlosas commented Oct 5, 2023

Good catch!
I can make the fix now, since there is a feature waiting to be released.

Thanks for the heads up :D

@carlosas
Copy link
Owner

carlosas commented Oct 5, 2023

@carlosas carlosas closed this as completed Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants