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 Level 2 #6161

Merged
merged 1 commit into from
Jul 1, 2020
Merged

PHPStan Level 2 #6161

merged 1 commit into from
Jul 1, 2020

Conversation

vv12131415
Copy link
Contributor

@vv12131415 vv12131415 commented Jun 21, 2020

Subject

I've bumped PHPStan to level 2. But since we can't break things most of the changes are baseline file and commented code in interfaces.

I am targeting this branch, because this is not BC break.

To do

  • Choose what error to thow in src/Controller/CRUDController.php; (no error, just isset check)
  • Is that ok for me to override properties only because Phpstan can understand (see CRUDController and *Mapper classes for examples). I think no, but this way PHPStan is thowing errors because base interface (BuilderInterface) doesn't have some methods that are required here (ShowBuilderInterface and it's addField method)
  • What to do with other errors that left; (no errors of PHPStan left)
 ------ --------------------------------------------------------------------------------------------- 
  Line   Command/QuestionableCommand.php                                                              
 ------ --------------------------------------------------------------------------------------------- 
  41     Call to an undefined method Symfony\Component\Console\Helper\QuestionHelper::getQuestion().  
  64     Call to an undefined method Symfony\Component\Console\Helper\QuestionHelper::getQuestion().  
 ------ --------------------------------------------------------------------------------------------- 

 ------ --------------------------------------------------------------------------------------------------- 
  Line   Security/Handler/AclSecurityHandler.php                                                            
 ------ --------------------------------------------------------------------------------------------------- 
  191    Call to an undefined method Symfony\Component\Security\Acl\Model\AclInterface::insertObjectAce().  
  213    Call to an undefined method Symfony\Component\Security\Acl\Model\AclInterface::insertClassAce().   
  215    Call to an undefined method Symfony\Component\Security\Acl\Model\AclInterface::updateClassAce().   
  220    Call to an undefined method Symfony\Component\Security\Acl\Model\AclInterface::deleteClassAce().   
 ------ --------------------------------------------------------------------------------------------------- 

 ------ -------------------------------------------------------------------------------------------------- 
  Line   Util/AdminAclManipulator.php                                                                      
 ------ -------------------------------------------------------------------------------------------------- 
  94     Call to an undefined method Symfony\Component\Security\Acl\Model\AclInterface::insertClassAce().  
  97     Call to an undefined method Symfony\Component\Security\Acl\Model\AclInterface::updateClassAce().  
  107    Call to an undefined method Symfony\Component\Security\Acl\Model\AclInterface::deleteClassAce().  
 ------ -------------------------------------------------------------------------------------------------- 

@vv12131415
Copy link
Contributor Author

this is ready for review/discussion

@vv12131415 vv12131415 marked this pull request as ready for review June 21, 2020 13:30
src/Action/SetObjectFieldValueAction.php Outdated Show resolved Hide resolved
src/Controller/CRUDController.php Show resolved Hide resolved
src/Controller/CRUDController.php Outdated Show resolved Hide resolved
src/Admin/AbstractAdmin.php Outdated Show resolved Hide resolved
src/Action/SetObjectFieldValueAction.php Outdated Show resolved Hide resolved
src/Admin/AdminInterface.php Outdated Show resolved Hide resolved
src/Admin/AdminInterface.php Show resolved Hide resolved
src/Command/ExplainAdminCommand.php Outdated Show resolved Hide resolved
src/EventListener/AssetsInstallCommandListener.php Outdated Show resolved Hide resolved
src/Form/FormMapper.php Show resolved Hide resolved
src/Maker/AdminMaker.php Show resolved Hide resolved
src/Util/AdminObjectAclData.php Show resolved Hide resolved
@VincentLanglet
Copy link
Member

In a general way, I think it would be better to avoid writing

/** @var Foo $foo */

And rely instead on

assert($foo instanceof Foo)

Or

if (!$foo instanceof Foo) {
  // do something else or throws an exception
}

Because if $foo HAVE TO be a FOO instance, it's better to check for it when the param could be an more generic Interface instead.

@vv12131415 vv12131415 force-pushed the phpstan-level-2 branch 2 times, most recently from df97170 to 1ad9203 Compare June 21, 2020 16:42
@vv12131415
Copy link
Contributor Author

Okay. Most of it is fixed. BUT I have those

 ------ --------------------------------------------------------------------------------------------- 
  Line   Command/QuestionableCommand.php                                                              
 ------ --------------------------------------------------------------------------------------------- 
  41     Call to an undefined method Symfony\Component\Console\Helper\QuestionHelper::getQuestion().  
  64     Call to an undefined method Symfony\Component\Console\Helper\QuestionHelper::getQuestion().  
 ------ --------------------------------------------------------------------------------------------- 


what should be done with them?

I can simply add them to baseline.
I haven't found any method like this one, on QuestionHelper::getQuestion

@vv12131415
Copy link
Contributor Author

vv12131415 commented Jun 21, 2020

Have no idea why phpstan-qa fails with this


 In Resolver.php line 429:
                                                                               
  Service 'rules.59' (type of PHPStan\Rules\Symfony\ContainerInterfacePrivate  
  ServiceRule): Multiple services of type PHPStan\Symfony\ServiceMap found: 0  
  270, 0293 (needed by $symfonyServiceMap in __construct())                    
                                                                               

In Autowiring.php line 50:
                                                                          
  Multiple services of type PHPStan\Symfony\ServiceMap found: 0270, 0293  
                                                                     

any idea?


It was phpstan-symfony plugin.
I'm removing it, since it requires container.xml

You have to provide a path to srcDevDebugProjectContainer.xml https://github.com/phpstan/phpstan-symfony#configuration

and breaks CI build

@vv12131415 vv12131415 force-pushed the phpstan-level-2 branch 2 times, most recently from bd0b591 to feb83c7 Compare June 21, 2020 18:19
src/Admin/AdminInterface.php Outdated Show resolved Hide resolved
src/Security/Handler/AclSecurityHandler.php Outdated Show resolved Hide resolved
src/EventListener/AssetsInstallCommandListener.php Outdated Show resolved Hide resolved
src/Admin/AbstractAdmin.php Outdated Show resolved Hide resolved
src/Command/ExplainAdminCommand.php Outdated Show resolved Hide resolved
src/Util/AdminAclManipulator.php Outdated Show resolved Hide resolved
src/Util/AdminObjectAclData.php Outdated Show resolved Hide resolved
@VincentLanglet
Copy link
Member

Okay. Most of it is fixed. BUT I have those

 ------ --------------------------------------------------------------------------------------------- 
  Line   Command/QuestionableCommand.php                                                              
 ------ --------------------------------------------------------------------------------------------- 
  41     Call to an undefined method Symfony\Component\Console\Helper\QuestionHelper::getQuestion().  
  64     Call to an undefined method Symfony\Component\Console\Helper\QuestionHelper::getQuestion().  
 ------ --------------------------------------------------------------------------------------------- 

what should be done with them?

I can simply add them to baseline.
I haven't found any method like this one, on QuestionHelper::getQuestion

I would change

new Question($questionHelper->getQuestion($questionText, $default), $default);

to

new Question($questionText, $default);

And deprecate the getQuestionHelper method.

I don't know how it could currently work.

@vv12131415 vv12131415 force-pushed the phpstan-level-2 branch 3 times, most recently from 85525d9 to d97cf14 Compare June 21, 2020 19:15
@vv12131415
Copy link
Contributor Author

And deprecate the getQuestionHelper method.

Can't deprecate it, since we need to QuestionHelper::ask()

@vv12131415 vv12131415 force-pushed the phpstan-level-2 branch 3 times, most recently from 0364ca9 to d837b12 Compare June 21, 2020 21:17
@vv12131415
Copy link
Contributor Author

vv12131415 commented Jun 21, 2020

can someone help with question
I don't get failures in tests when calling assert I even tried something like this
php -d zend.assertions=-1 -d assert.exception=1 vendor/bin/simple-phpunit -c phpunit.xml.dist tests/Security/Handler/AclSecurityHandlerTest.php

  1. How should I configure my local enviroment?
  2. How to test against assert?

upd: note to self use something like this

php -d zend.assertions=1 -d assert.exception=0 vendor/bin/simple-phpunit -c phpunit.xml.dist

@vv12131415 vv12131415 force-pushed the phpstan-level-2 branch 5 times, most recently from da5a930 to 88cc4a4 Compare June 22, 2020 22:48
@vv12131415
Copy link
Contributor Author

@VincentLanglet now it's finished

@OskarStark OskarStark changed the title Phpstan level 2 PHPStan Level 2 Jun 23, 2020
VincentLanglet
VincentLanglet previously approved these changes Jun 29, 2020
src/Util/AdminAclManipulator.php Outdated Show resolved Hide resolved
src/Util/AdminObjectAclData.php Outdated Show resolved Hide resolved
tests/Admin/AdminTest.php Outdated Show resolved Hide resolved
tests/Admin/AdminTest.php Outdated Show resolved Hide resolved
tests/Admin/AdminTest.php Outdated Show resolved Hide resolved
tests/Command/ExplainAdminCommandTest.php Outdated Show resolved Hide resolved
tests/Command/ExplainAdminCommandTest.php Outdated Show resolved Hide resolved
tests/Security/Handler/AclSecurityHandlerTest.php Outdated Show resolved Hide resolved
tests/Security/Handler/AclSecurityHandlerTest.php Outdated Show resolved Hide resolved
src/Admin/AdminInterface.php Show resolved Hide resolved
src/Command/ExplainAdminCommand.php Outdated Show resolved Hide resolved
src/Admin/AbstractAdmin.php Outdated Show resolved Hide resolved
src/Security/Handler/AclSecurityHandler.php Outdated Show resolved Hide resolved
@phansys
Copy link
Member

phansys commented Jun 30, 2020

I noticed there are some new exceptions. Is yet "pedantic" the right label? Don't we need a changelog for these exceptions? Are they respecting BC?

@VincentLanglet
Copy link
Member

I noticed there are some new exceptions. Is yet "pedantic" the right label? Don't we need a changelog for these exceptions? Are they respecting BC?

If you now pass by these exception the code wasn't working before.

This situation is always similar to this

$metadata = $this->validator->getMetadataFor($this->getClass());
if (!$metadata instanceof GenericMetadata) {
    throw new \Exception();
}

$metadata->someMethodThatOnlyGenericMetadataHas();

@phansys
Copy link
Member

phansys commented Jun 30, 2020

I understand, although I guess a border case could be reached if the previous exception has a different type than the new one and the user is relying on these types.

@VincentLanglet
Copy link
Member

VincentLanglet commented Jun 30, 2020

I understand, although I guess a border case could be reached if the previous exception has a different type than the new one and the user is relying on these types.

Indeed. Previously this was an error, now it's an exception. But nobody should catch an error. So IMHO this would be such a border case and a wrong usage of these methods, that this shouldn't be under the BC-promises.
We should be able to update the Phpstan level without a new major version.

@phansys
Copy link
Member

phansys commented Jun 30, 2020

We should be able to update the Phpstan level without a new major version.

Agree. I'm just saying that an upgrade note should help to cover such edge scenarios in case they are met.

jordisala1991
jordisala1991 previously approved these changes Jun 30, 2020
src/Admin/FieldDescriptionInterface.php Show resolved Hide resolved
tests/Command/ExplainAdminCommandTest.php Outdated Show resolved Hide resolved
tests/Security/Handler/AclSecurityHandlerTest.php Outdated Show resolved Hide resolved
tests/Admin/AdminTest.php Outdated Show resolved Hide resolved
added some tests (due to changes that where made because of PHPStan)
@vv12131415
Copy link
Contributor Author

@phansys any chance to get this merged?

Copy link
Member

@phansys phansys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, since there are new exceptions thrown, an upgrade note should be required.
Regardless that, I think the PR is RTM.

@greg0ire greg0ire merged commit 38b3377 into sonata-project:3.x Jul 1, 2020
@greg0ire
Copy link
Contributor

greg0ire commented Jul 1, 2020

Thanks @vladyslavstartsev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants