Skip to content

PHPStan add support of magic methods of Data Object. #27905

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

Conversation

swnsma
Copy link
Contributor

@swnsma swnsma commented Apr 20, 2020

  • Add extension to support of 'get/set/has/uns' 'magic' methods (with usage of __call) for DataObject;
  • Add support for SessionManager (it forwards all calls to DataObject container);
  • Add test coverage for extensions;
  • fix outdated tests for Filtered Error check;
  • increase PHPStan check level 0 -> 1.

Q:

  • Are tests from dev/tests/static/framework/tests folder also included to test builds?

Resolved issues:

  1. resolves [Issue] PHPStan add support of magic methods of Data Object. #28303: PHPStan add support of magic methods of Data Object.

oleksandrkravchuk added 2 commits April 20, 2020 15:39
Add support of magic mathods of Session Manager (which uses Data Object as a container).
Increase PHPStan level to 1.
Add tests to test DataObjectReflectionExtension.
@m2-assistant
Copy link

m2-assistant bot commented Apr 20, 2020

Hi @swnsma. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Guide documentation.

@magento-engcom-team magento-engcom-team added Release Line: 2.4 Partner: Vaimo Pull Request is created by partner Vaimo partners-contribution Pull Request is created by Magento Partner labels Apr 20, 2020
@lenaorobei lenaorobei requested a review from viktym April 20, 2020 15:52
Copy link
Contributor

@viktym viktym left a comment

Choose a reason for hiding this comment

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

Hi @swnsma
Thank you for your contribution
Increasing the PHPStan level from 0 to 1 involves activation next additional rules and checks:

parameters:
	checkMaybeUndefinedVariables: true
	checkExtraArguments: true
	reportMagicMethods: true
	reportMagicProperties: true


conditionalTags:
	PHPStan\Rules\Variables\VariableCertaintyNullCoalesceRule:
		phpstan.rules.rule: %featureToggles.nullCoalesce%

rules:
	- PHPStan\Rules\Classes\UnusedConstructorParametersRule
	- PHPStan\Rules\Constants\ConstantRule
	- PHPStan\Rules\Functions\UnusedClosureUsesRule
	- PHPStan\Rules\Variables\VariableCertaintyInIssetRule

services:
	-
		class: PHPStan\Rules\Variables\VariableCertaintyNullCoalesceRule

Can you run PHPStan check using level 1 on CE/EE/B2B Magento codebase and compare results with level 0 (create a diff) ?
Please, make sure that all reported errors can be fixed when Magento developers will face them in their PRs. Or, possibly, some files should be added in blacklist at once.
Also, please check if new PHPStan checks intersect with PHPMD and PHPCS checks. We should avoid double suppression (like unused method argument etc.).

@ghost ghost assigned viktym Apr 20, 2020
@swnsma
Copy link
Contributor Author

swnsma commented Apr 22, 2020

Hi, @viktym!

Please find the diff: https://www.diffchecker.com/rHxwkJVM
In total there are 1604 vs 2356 errors.
I have used CE + MSI + EE + B2B codebase (lib + app/code + dev + setup).
From the diff I can sum up that for Magento relevant next new checks:

  • incorrect number of arguments passed;
  • a variable might not be defined check;
  • isset usage;
  • access to the undefined property;
  • anonymous function and use and closure;
  • call to the undefined method;
  • find undefined constants;

From all these violations, I see problems with constants (most of the problems are related to tests).

  1. Constants which is defined by tests settings:
  • TESTS_WEB_API_ADAPTER
  • TESTS_BASE_URL
  • TESTS_XDEBUG_ENABLED
  • TESTS_XDEBUG_SESSION
    As they did not declare in PHP directly, I suggest adding these constants to "ignoreErrors" in phpstan.neon
  1. Constants defined by tests fixtures:
  • STAGING_UPDATE_FIXTURE_ID
  • FIXTURE_ATTRIBUTE_USER_DEFINED_CUSTOMER_NAME
  • FIXTURE_ATTRIBUTE_USER_DEFINED_CUSTOMER_FRONTEND_LABEL
    I suggest to skip them and just use "phpstan:ignore"
  1. Constants, defined by tests setup:
  • INTEGRATION_TESTS_DIR
  • MAGENTO_MODULES_PATH
  • TESTS_MODULES_PATH
  • TESTS_INSTALLATION_DB_CONFIG_FILE
    These constants are defined on the fly and PHPStan needs to have files with constants definition in autoload.
    We need to autoload: dev/tests/setup-integration/framework/bootstrap.php and dev/tests/api-functional/framework/bootstrap.php
    But I suggest also to add these constants to "ignoreErrors".
  1. Constants defined by PHP CS
  • T_STRING_CONCAT
  • T_OPEN_CURLY_BRACKET
  • T_CLOSE_CURLY_BRACKET
  • T_OPEN_PARENTHESIS
  • T_CLOSE_PARENTHESIS
  • T_STYLE
  • T_ASPERAND
  • T_OPEN_CURLY_BRACKET
  • T_CLOSE_CURLY_BRACKET
  • T_OPEN_CURLY_BRACKET
  • T_STYLE
  • T_SEMICOLON
  • T_ASPERAND
  • T_COLON
  • T_SEMICOLON
  • T_COLON
  • T_STYLE
    All these constants are defined in vendor/squizlabs/php_codesniffer/src/Util/Tokens.php
    I am not sure in this point, probably, we can include this file to autoload.
  1. Constants from file dev/tools/UpgradeScripts/pre_composer_update_2.3.php
  • INFO
  • WARN
  • ERROR
    I suggest skipping this point.

What do you think about this?

@viktym
Copy link
Contributor

viktym commented Apr 28, 2020

@swnsma great job!
I'll review the diff and provide feedback till the end of the week

@viktym
Copy link
Contributor

viktym commented Apr 30, 2020

Hi @swnsma
I agree with your suggestions 1-3

Also, I recommend to ignore constants from 4 using pattern: #Constant T_[A-Z_]+ not found.#

Number 5 you can add in blacklist:

dev/tests/static/testsuite/Magento/Test/Php/_files/phpstan/blacklist/common.txt

Please proceed with implementation of these recommendations and fix the failed Integrity test as well:

Passed: 32740, Failed: 1, Incomplete: 0, Skipped: 0.
Data set: dev/tests/static/framework/tests/unit/testsuite/Magento/PhpStan/Reflection/DataObject/MagicMethodsTest.php
Location of /var/www/html/dev/tests/static/framework/tests/unit/testsuite/Magento/PhpStan/Reflection/DataObject/MagicMethodsTest.php does not match formal namespace: Magento\PhpStan\Reflection\DataObject

oleksandrkravchuk and others added 2 commits May 2, 2020 09:30
@swnsma
Copy link
Contributor Author

swnsma commented May 2, 2020

Hi @viktym!
Done!

@viktym
Copy link
Contributor

viktym commented May 6, 2020

Hi @swnsma
PR looks good for me
Don't have any objections

@ghost ghost assigned lenaorobei May 6, 2020
oleksandrkravchuk added 2 commits May 7, 2020 09:18
….com:swnsma/magento2-1 into PHPStan-support-of-Data-Object-magic-methods
@ghost ghost assigned swnsma May 7, 2020
@lenaorobei
Copy link
Contributor

@magento run all tests

@slavvka slavvka added the Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. label May 20, 2020
@slavvka slavvka added this to the 2.4.1 milestone May 20, 2020
@swnsma
Copy link
Contributor Author

swnsma commented Jun 24, 2020

@magento run all tests

@lenaorobei
Copy link
Contributor

@magento run all tests

@engcom-Charlie
Copy link
Contributor

@magento run all tests

@engcom-Alfa engcom-Alfa added QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope and removed QA: Ready to add to Regression Scope Should be analyzed and added to Regression Testing Scope(if applicable) labels Aug 5, 2020
@engcom-Charlie
Copy link
Contributor

HI @swnsma, could you please look through a failed static tests?

@swnsma
Copy link
Contributor Author

swnsma commented Aug 5, 2020

Fixed.

BTW.
I think this PR, at least, deserves 'Award: test coverage' label, as it introduce tests coverage for the new functionality.

@engcom-Charlie
Copy link
Contributor

@magento run all tests

@swnsma
Copy link
Contributor Author

swnsma commented Aug 5, 2020

@magento run all tests

@m2-assistant
Copy link

m2-assistant bot commented Aug 8, 2020

Hi @swnsma, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: advanced Award: MFTF test coverage Partner: Vaimo Pull Request is created by partner Vaimo partners-contribution Pull Request is created by Magento Partner Priority: P3 May be fixed according to the position in the backlog. Progress: accept QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope Release Line: 2.4 Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Issue] PHPStan add support of magic methods of Data Object.
7 participants