Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 8a9cd8c

Browse files
authored
Merge pull request #8547 from magento/db_static_analysis
Adding static analysis topic from wiki
2 parents 76bffc1 + a6f7fd5 commit 8a9cd8c

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

src/_data/toc/testing.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pages:
7070
- label: Running Static Tests
7171
url: /test/static/static_test_execution.html
7272

73+
- label: Setting up for Static Analysis
74+
url: /test/static/static-analysis.html
75+
7376
- label: Semantic Version Checker
7477
children:
7578

281 KB
Loading

src/common/images/static-eslint.png

108 KB
Loading

src/common/images/static-jscs.png

99.9 KB
Loading

src/common/images/static-md.png

271 KB
Loading
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
group: testing
3+
title: Setting up static analysis
4+
functional_areas:
5+
- Testing
6+
- test
7+
---
8+
9+
## Introduction
10+
11+
This topic provides steps to set up most static analyzers that are used in our build pipeline to work in your local PHPStorm environment. This guide is written specifically for those using PHPStorm on MacOS.
12+
13+
## Before you begin
14+
15+
If you have not already, verify that `node` and `npm` are installed. Then, in the Magento root directory, run `npm install`. After installation, ensure there is a `node_modules` directory in the root of your project before proceeding.
16+
17+
{:bs-callout-info}
18+
For all of the static configuration installations below involving the PHPStorm Preferences dialog, make sure you are clicking the "Apply" button before clicking "OK" to close the Preferences dialog.
19+
20+
## Javascript Code Style check
21+
22+
{:.bs-callout-info}
23+
JSCS is deprecated in PHPStorm 2020.2. You must install the JSCS plugin.
24+
25+
{:.procedure}
26+
To install the JSCS plugin:
27+
28+
1. Go to PHPStorm preferences > Languages & Frameworks > JavaScript > Code Quality Tools > JSCS.
29+
1. Check **Enable**, if it is unchecked.
30+
1. Enter the path to your node binary (the result of outputting `which node` in your terminal).
31+
1. Enter the path to your JSCS package: `[magento_root]/node_modules/jscs`
32+
1. Click **Configuration File** and in the adjacent input field enter the path to the JSCS file Magento uses, which is located in: `dev/tests/static/testsuite/Magento/Test/Js/_files/jscs/.jscsrc`
33+
34+
![JSCS config]({{site.baseurl}}/common/images/static-jscs.png)
35+
36+
To verify it works, in any JS file add `snake_case: [],` to any object literal, and you should see a warning about not using camel case for the object key.
37+
38+
## ESLint
39+
40+
1. Go to PHPStorm preferences > Languages & Frameworks > JavaScript > Code Quality Tools > ESLint.
41+
1. Click **Manual ESLint configuration**.
42+
1. Fill in the adjacent input fields with the path to your node binary (the result of outputting `which node` in your terminal).
43+
1. Enter the path to your ESLint package: `[magento_root]/node_modules/eslint`
44+
1. Click **Configuration File** and in the adjacent input field enter the path to the ESLint file Magento uses, which is located in `dev/tests/static/testsuite/Magento/Test/Js/_files/eslint/.eslintrc-magento`.
45+
46+
See the image below for example configuration:
47+
48+
![ESLint]({{site.baseurl}}/common/images/static-eslint.png)
49+
50+
To verify it works, in any JS file add `/** Hello world */` as a doc comment to any method, and you should see a warning about the comment being on one line.
51+
52+
## PHPCS
53+
54+
1. Go to PHPStorm preferences > Languages & Frameworks > PHP > Quality Tools > PHP_CodeSniffer.
55+
1. Click the `...` button to bring up another configuration modal.
56+
1. Enter the PHP_CodeSniffer path: `[magento_root]/vendor/bin/phpcs`
57+
58+
### Configuring for Magento Coding Standard
59+
60+
1. Go to the [Magento Coding Standard GitHub Repository](https://github.com/magento/magento-coding-standard)
61+
1. Follow instructions within the README to install the Magento Coding Standard for PHPCS. Verify it is installed with `vendor/bin/phpcs -i`. You should see `Magento2` in the output.
62+
1. Go to PHPStorm preferences > Editor > Inspections, and in the adjacent window go to PHP > Quality Tools > PHP_CodeSniffer validation.
63+
1. Under **Coding Standard** dropdown, select `Magento2`.
64+
65+
See the image below for example configuration:
66+
67+
![PHPCS]({{site.baseurl}}/common/images/static-codesniff.png)
68+
69+
To verify it works, add the following PHP snippet: `$base = basename($_SERVER['SCRIPT_FILENAME']);`. You should see a warning that the use of `basename` is forbidden, as well as the use of _superglobals_.
70+
71+
## PHPMD
72+
73+
1. Go to PHPStorm preferences > Languages & Frameworks > PHP > Quality Tools > Mess Detector.
74+
1. Click the `...` button to bring up another configuration modal.
75+
1. Enter the PHP Mess Detector path: `[magento_root]/vendor/phpmd/phpmd/src/bin/phpmd`
76+
1. Go to PHPStorm preferences > Editor > Inspections and in the adjacent window go to > PHP > Quality Tools > PHP Mess Detector validation.
77+
1. Under custom rulesets, add the path to the PHPMD ruleset Magento uses: `[magento_root]/dev/tests/static/testsuite/Magento/Test/Php/_files/phpmd/ruleset.xml`
78+
79+
See the image below for example configuration:
80+
81+
![PHPMD]({{site.baseurl}}/common/images/static-md.png)
82+
83+
To verify it works, add an unused private method to a class, and you should see a warning from PHPMD about it not being used.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../v2.3/test/static/static-analysis.md

0 commit comments

Comments
 (0)