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

[shopsys] automated fixes and additions of annotations for extended classes #1344

Merged
merged 27 commits into from Sep 5, 2019

Conversation

vitek-rostislav
Copy link
Contributor

@vitek-rostislav vitek-rostislav commented Aug 22, 2019

Q A
Description, reason for the PR When using class inheritance (and interface implementations) for extending the behavior of the framework from project-base, static analysis tools are not able to understand the extensions, which makes the DX worse and PHPStan inspections fail. We want to use the higher level of PHPStan even in project-base and do not force the project developers to do the boring things manually so we created the automated tool that makes the static analysis understand the extended code properly.
New feature Yes
BC breaks No
Fixes issues closes #914
Have you read and signed our License Agreement for contributions? Yes

TODO

  • add docs

Copy link
Member

@grossmannmartin grossmannmartin left a comment

Choose a reason for hiding this comment

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

So far reviewed first three commits

Copy link
Member

@grossmannmartin grossmannmartin left a comment

Choose a reason for hiding this comment

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

Hi @vitek-rostislav, I finished the review.

First of all, I'm really thrilled about this new functionality! I really think it helps a lot.

I made some suggestions and comments to the code, please check them out.

docs/introduction/framework-extensibility.md Outdated Show resolved Hide resolved
docs/introduction/framework-extensibility.md Outdated Show resolved Hide resolved
docs/introduction/framework-extensibility.md Outdated Show resolved Hide resolved
@vitek-rostislav vitek-rostislav force-pushed the rv-extended-classes-fixer branch 4 times, most recently from 9cae3fe to a6c0c6b Compare August 27, 2019 16:22
@vitek-rostislav vitek-rostislav force-pushed the rv-extended-classes-fixer branch 6 times, most recently from 76f040e to e8d18c2 Compare September 2, 2019 19:17
Copy link
Member

@grossmannmartin grossmannmartin left a comment

Choose a reason for hiding this comment

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

Hi, awesome job with refactoring!

I reviewed the changes excluding the tests and have few things to consider.
In the end, don't forget to add changes generated with this command (but it will be better after the command itself is reviewed and tested – another PR maybe?)

And PHPStan fails on dummy classes for tests, these could be excluded from analysis (maybe renamed like ChildClass3.php.test but I'm not sure if it will work.

I take a look at tests tomorrow

Copy link
Member

@grossmannmartin grossmannmartin left a comment

Choose a reason for hiding this comment

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

finished the review. just last thing about phpstan ignored rule

packages/framework/phpstan.neon Outdated Show resolved Hide resolved
phpstan.neon Outdated Show resolved Hide resolved
Copy link
Contributor

@LukasHeinz LukasHeinz left a comment

Choose a reason for hiding this comment

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

Summary of business validation discussion:

  • there should be two separate upgrade notes - one for using annotation fix and second for lvl4 PHP stan because these are two separate issues
  • the annotation-fix should be part of coding standards (or something like this) and should behave the same way (no fixes without confirmation)

Copy link
Member

@grossmannmartin grossmannmartin left a comment

Choose a reason for hiding this comment

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

Hi, I will go to hell for more and more requested changes

docs/introduction/framework-extensibility.md Outdated Show resolved Hide resolved
packages/framework/build.xml Outdated Show resolved Hide resolved
docs/introduction/framework-extensibility.md Outdated Show resolved Hide resolved
docs/introduction/framework-extensibility.md Outdated Show resolved Hide resolved
Copy link
Member

@grossmannmartin grossmannmartin left a comment

Choose a reason for hiding this comment

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

Reviewed last two commits

…ject classes that extend the classes (or implement interfaces) from framework

- the registry will be used for automated annotations fixes and additions in project classes
- this will make PHPStan and PHPStorm understand the code of Shopsys Framework based project properly
- the extensions information is not collected in production environment as it is not necessary there
- the class is unit tested
- AnnotationsReplacementsMap was extracted as well because the mappings are used on multiple places in the command
… replaceInPropertyType() extracted to AnnotationsReplacer

- added unit tests for the methods
…r annotations replacements

- getRelevantFrameworkAnnotationsPattern() was broken by rewritting the history of the original commit
- the functionality extracted to AnnotationsReplacementsMap::getPatternForAny
- AnnotationsReplacementsMap is an autowired DIC service as well
  - it's constructor's argument $classExtensionMap is injected using expression language
  - see https://symfony.com/doc/3.4/service_container/expression_language.html
…and @method annotation extracted to separate classes

- MethodAnnotationsFactory - responsible for providing @method annotation lines, unit tested
- PropertyAnnotationsFactory - responsible for providing @Property annotation lines, unit tested
- AnnotationsAdder - respnsible for adding the annotations to classes, unit tested
- FileContentsReplacer - a simple helper class wrapping file_get_contents, str_replace, and file_put_contents calls sequence
- all the automated fixes and additions of annotations are applied
- the standards are failing intentionally on this commit as there are several phpstan rules violations that need to be fixed manually yet
…matically using "php phing fix-annotations"

- sometimes, there is an extended entity returned directly by calling a framework service thanks to EntityNameResolver magic that phpstan does not understand
- there is a workaround for this - a project developer can create an empty service extending the framework one (e.g. ShopBundle\ProductFacade extends FrameworkBundle\BaseProductFacade)
	- in such a case the developer can generate annotations for his extended class using "php phing fix-annotations" that will wix the phpstan rules violations as well
Copy link
Member

@grossmannmartin grossmannmartin left a comment

Choose a reason for hiding this comment

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

Reviewed changes from testing.
One thing and you have typo in the commit message

- ClassExtensionRegistry: extended ontrollers are now registered as well
+ ClassExtensionRegistry: extended controllers are now registered as well

…rbose output

- FixExtendedClassesAnnotationsCommand renamed to ExtendedClassesAnnotationsCommand
- filenames that were fixed (or need to be fixed when using "dry-run" mode) are reported
…hing targets "standards", "standards-diff", "standards-fix", and "standards-fix-diff"

- there is a new build property "check-and-fix-annotations" that allows project developers to skip the checks and fixes of annotations when they do not want to include them in their "standards-*" targets
- done in such a way to avoid possible BC breaks
…into account as well

- when a method has a parameter of a type that is extended in project, appropriate @method annotation is created
- proper parameter types are included in @method annotations
- done to achieve correct IDE autocompletion
- parameter types are now taken into account as well
…the annotations fixer

- proper format is "/** @var Type $variableName */" above the variable
- the annotations were malformatted before so we fixed what we noticed :)
…tions-*" targets moved to "Coding Standards section"
@vitek-rostislav vitek-rostislav dismissed LukasHeinz’s stale review September 5, 2019 07:31

All the objections were addressed, approved personally 😁

@vitek-rostislav vitek-rostislav merged commit bd0ccc5 into master Sep 5, 2019
@vitek-rostislav vitek-rostislav deleted the rv-extended-classes-fixer branch September 5, 2019 07:32
@simara-svatopluk
Copy link
Contributor

🚀

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

Successfully merging this pull request may close these issues.

Using extended classes in project
6 participants