magento / magento/magento-coding-standard
[New Rule] Add declare(strict_types=1) to your PHP files
- Dominant language
- PHP
- Stars
- 375
- Forks
- 165
- PR merge metrics
- No merged PRs in 30d
Description
### Rule
All new PHP files MUST have strict type mode enabled by starting with `declare(strict_types=1)`;. All updated PHP files SHOULD have strict type mode enabled. PHP interfaces SHOULD NOT have this declaration.
### Reason
Source: [Magento Technical Guidelines](https://devdocs.magento.com/guides/v2.3/coding-standards/technical-guidelines.html).
With PHP 7, it is possible to add type hinting to your code. However, this doesn't mean that types are actually enforced, unless strict typing is enabled by adding `declare(strict_types=1)` to the top of each PHP file.
PHP code becomes more robust when type hinting (argument types, return types) are added. With the `declare(strict_types=1)` added, there is less chance for bugs that related to type casting.
### Implementation
Please refer to [ExtDN](https://github.com/extdn/extdn-phpcs/blob/master/Extdn/Sniffs/Classes/StrictTypesSniff.php) an [generic PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer/pull/1771) (not merged yet) implementations.
### Important details
This sniff should be applicable to Magento >=2.2, because older versions of Magento supports PHP 5.5 and 5.6.
Follow[ the discussion about version specific sniffs](https://github.com/magento/magento-coding-standard/issues/17) for more details.
Contributor guide
Assessment
This issue has not been assessed yet.