没有不值得去解决的问题,也没有不值得去学习的技术!

Validate Code Against the Yii 2 Web Framework Coding Standard With PHP CodeSniffer in PhpStorm on Windows 10 64-Bit

Configure the PHP code sniffer as a PhpStorm inspection: File | Settings → Editor → Inspections → PHP → PHP Code Sniffer validation

1. Reference URL: https://github.com/yiisoft/yii2-coding-standards , as shown in Figure 1

Reference URL: https://github.com/yiisoft/yii2-coding-standards
Figure 1

2. Clone the yiisoft/yii2-coding-standards repository, as shown in Figure 2

Clone the yiisoft/yii2-coding-standards repository
Figure 2

3. Navigate to the E:\wwwroot\yii2-coding-standards directory and run the installation, as shown in Figure 3
cd .\yii2-coding-standards\
composer install

Navigate to the E:\wwwroot\yii2-coding-standards directory and run the installation,
Figure 3

4. Configure the local PHP interpreter by clicking the button next to the File | Settings → Languages & Frameworks → PHP → CLI Interpreter drop-down list, as shown in Figure 4

Configure the local PHP interpreter by clicking the button next to the File | Settings → Languages & Frameworks → PHP → CLI Interpreter drop-down list
Figure 4

5. CLI Interpreters → Add → Other Local. If you have already configured a local interpreter in PhpStorm, it will also appear in the menu, and the menu item will change to Other Local, as shown in Figure 5

CLI Interpreters → Add → Other Local. If you have already configured a local interpreter in PhpStorm, it will also appear in the menu, and the menu item will change to Other Local
Figure 5

6. CLI Interpreters → Add → Other Local → Local → OK, as shown in Figure 6

CLI Interpreters → Add → Other Local → Local → OK
Figure 6

7. Set the local PHP interpreter to Local, as shown in Figure 7

Set the local PHP interpreter to Local
Figure 7

8. Select the code sniffer script to use by clicking the button next to the File | Settings → Languages & Frameworks → PHP → Code Sniffer → Configuration: Local drop-down list, as shown in Figure 8

Select the code sniffer script to use by clicking the button next to the File | Settings → Languages & Frameworks → PHP → Code Sniffer → Configuration: Local drop-down list
Figure 8

9. File | Settings → Languages & Frameworks → PHP → Code Sniffer → PHP Code Sniffer path: E:\wwwroot\yii2-coding-standards\vendor\bin\phpcs.bat, as shown in Figure 9
Reference command: ./vendor/bin/phpcs –extensions=php –standard=Yii2 /home/resurtm/work/Yii2MegaApp/

File | Settings → Languages & Frameworks → PHP → Code Sniffer → PHP Code Sniffer path:E:\wwwroot\yii2-coding-standards\vendor\bin\phpcs.bat
Figure 9

10. Click the Validate button. If validation passes successfully, PhpStorm will display the detected code sniffer version information, as shown in Figure 10

Click the Validate button. If validation passes successfully, PhpStorm will display the detected code sniffer version information
Figure 10

11. The local code sniffer script is successfully configured, as shown in Figure 11

The local code sniffer script is successfully configured
Figure 11

12. Configure the PHP code sniffer as a PhpStorm inspection: File | Settings → Editor → Inspections → PHP → PHP Code Sniffer validation, as shown in Figure 12

Configure the PHP code sniffer as a PhpStorm inspection: File | Settings → Editor → Inspections → PHP → PHP Code Sniffer validation
Figure 12

13. Yii uses PSR-1 and PSR-2 as code style standards: File | Settings → Editor → Code Style → PHP → Set from… → Predefined Style → PSR1/PSR2, as shown in Figure 13

Yii uses PSR-1 and PSR-2 as code style standards: File | Settings → Editor → Code Style → PHP → Set from... → Predefined Style → PSR1/PSR2
Figure 13

14. Run the code sniffer in batch mode: Code | Inspect code → Inspection scope: Whole project, as shown in Figure 14

Run the code sniffer in batch mode: Code | Inspect code → Inspection scope: Whole project
Figure 14

15. View the inspection results in the Inspection Results Tool Window. Errors and warnings reported by the PHP code sniffer have the phpcs prefix to distinguish them from PhpStorm’s internal inspections, as shown in Figure 15

View the inspection results in the Inspection Results Tool Window. Errors and warnings reported by the PHP code sniffer have the phpcs prefix to distinguish them from PhpStorm's internal inspections
Figure 15

16. Click Group by Severity to prioritize resolving the highest severity issues, as shown in Figure 16

Server problem
Typo
Info
Weak Warning
Warning
Error
Unused entry

Click Group by Severity to prioritize resolving the highest severity issues
Figure 16

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系