Skip to content

Commit 0338636

Browse files
committed
[PSR-2 Compliance] Fix #8612: Hundreds of PHPCS-based static tests violations in mainline
- enable PHPCS-based tests for the whole codebase - combine three PHPCS-based tests into one - polish up Magento Coding Standard into one ruleset.xml: no new rules added, only those rules removed which are already contained in PSR2 - run static tests under PHP 7 which is ~3 times faster than PHP 5.6
1 parent 65d8a67 commit 0338636

File tree

6 files changed

+34
-136
lines changed

6 files changed

+34
-136
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ cache:
2626
directories: $HOME/.composer/cache
2727
matrix:
2828
exclude:
29-
- php: 7.0
29+
- php: 5.6.29
3030
env: TEST_SUITE=static
3131
before_install: ./dev/travis/before_install.sh
3232
install: composer install --no-interaction --prefer-dist
3333
before_script: ./dev/travis/before_script.sh
3434
script:
35-
- cd dev/tests/$TEST_SUITE
3635
- test $TEST_SUITE = "static" && TEST_FILTER='--filter "Magento\\Test\\Php\\LiveCodeTest"' || true
37-
- phpunit $TEST_FILTER
36+
- phpunit -c dev/tests/$TEST_SUITE $TEST_FILTER

dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/CodeSniffer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class CodeSniffer implements ToolInterface, ExtensionInterface
4040
*
4141
* @var array
4242
*/
43-
private $extensions = ['php'];
43+
private $extensions = ['php', 'phtml'];
4444

4545
/**
4646
* Constructor
@@ -51,8 +51,11 @@ class CodeSniffer implements ToolInterface, ExtensionInterface
5151
*/
5252
public function __construct($rulesetDir, $reportFile, Wrapper $wrapper)
5353
{
54-
$this->reportFile = $reportFile;
5554
$this->rulesetDir = $rulesetDir;
55+
if (!file_exists($rulesetDir) && file_exists($fullPath = realpath(__DIR__ . '/../../../../' . $rulesetDir))) {
56+
$this->rulesetDir = $fullPath;
57+
}
58+
$this->reportFile = $reportFile;
5659
$this->wrapper = $wrapper;
5760
}
5861

dev/tests/static/framework/Magento/ruleset.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
-->
88
<ruleset name="Magento">
99
<description>Custom Magento coding standard.</description>
10+
11+
<rule ref="PSR2"/>
12+
1013
<rule ref="Magento.Files.LineLength">
1114
<properties>
1215
<property name="lineLimit" value="120"/>
@@ -16,4 +19,11 @@
1619
<rule ref="Magento.LiteralNamespaces.LiteralNamespaces">
1720
<exclude-pattern>*/_files/*</exclude-pattern>
1821
</rule>
22+
23+
<rule ref="Generic.Functions.CallTimePassByReference"/>
24+
<rule ref="Generic.PHP.DeprecatedFunctions"/>
25+
26+
<rule ref="Squiz.Commenting.DocCommentAlignment"/>
27+
<rule ref="Squiz.Functions.GlobalFunction"/>
28+
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing"/>
1929
</ruleset>

dev/tests/static/testsuite/Magento/Test/Php/LiveCodeTest.php

Lines changed: 10 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -189,101 +189,27 @@ function ($file) use ($fileHasAllowedType, $fileIsInAllowedDirectory) {
189189
}
190190

191191
/**
192-
* Run the PSR2 code sniffs on the code
192+
* Retrieves full list of codebase paths without any files/folders filtered out
193193
*
194-
* @TODO: combine with testCodeStyle
195-
* @return void
194+
* @return array
196195
*/
197-
public function testCodeStylePsr2()
196+
private function getFullWhitelist()
198197
{
199-
$reportFile = self::$reportDir . '/phpcs_psr2_report.txt';
200-
$wrapper = new Wrapper();
201-
$codeSniffer = new CodeSniffer('PSR2', $reportFile, $wrapper);
202-
if (!$codeSniffer->canRun()) {
203-
$this->markTestSkipped('PHP Code Sniffer is not installed.');
204-
}
205-
if (version_compare($wrapper->version(), '1.4.7') === -1) {
206-
$this->markTestSkipped('PHP Code Sniffer Build Too Old.');
207-
}
208-
209-
$result = $codeSniffer->run(self::getWhitelist());
210-
211-
$output = "";
212-
if (file_exists($reportFile)) {
213-
$output = file_get_contents($reportFile);
214-
}
215-
$this->assertEquals(
216-
0,
217-
$result,
218-
"PHP Code Sniffer has found {$result} error(s): " . PHP_EOL . $output
219-
);
198+
return Files::init()->readLists(__DIR__ . '/_files/whitelist/common.txt');
220199
}
221200

222-
/**
223-
* Run the magento specific coding standards on the code
224-
*
225-
* @return void
226-
*/
227-
public function testCodeStyle()
201+
public function testNoViolationsDetectedByPhpCodeSniffer()
228202
{
229203
$reportFile = self::$reportDir . '/phpcs_report.txt';
230-
$wrapper = new Wrapper();
231-
$codeSniffer = new CodeSniffer(realpath(__DIR__ . '/_files/phpcs'), $reportFile, $wrapper);
232-
if (!$codeSniffer->canRun()) {
233-
$this->markTestSkipped('PHP Code Sniffer is not installed.');
234-
}
235-
$codeSniffer->setExtensions(['php', 'phtml']);
236-
$result = $codeSniffer->run(self::getWhitelist(['php', 'phtml']));
237-
238-
$output = "";
239-
if (file_exists($reportFile)) {
240-
$output = file_get_contents($reportFile);
241-
}
242-
243-
$this->assertEquals(
244-
0,
245-
$result,
246-
"PHP Code Sniffer has found {$result} error(s): " . PHP_EOL . $output
247-
);
248-
}
249-
250-
/**
251-
* Run the annotations sniffs on the code
252-
*
253-
* @return void
254-
* @todo Combine with normal code style at some point.
255-
*/
256-
public function testAnnotationStandard()
257-
{
258-
$reportFile = self::$reportDir . '/phpcs_annotations_report.txt';
259-
$wrapper = new Wrapper();
260-
$codeSniffer = new CodeSniffer(
261-
realpath(__DIR__ . '/../../../../framework/Magento/ruleset.xml'),
262-
$reportFile,
263-
$wrapper
264-
);
265-
if (!$codeSniffer->canRun()) {
266-
$this->markTestSkipped('PHP Code Sniffer is not installed.');
267-
}
268-
269-
$result = $codeSniffer->run(self::getWhitelist(['php']));
270-
$output = "";
271-
if (file_exists($reportFile)) {
272-
$output = file_get_contents($reportFile);
273-
}
204+
$codeSniffer = new CodeSniffer('Magento', $reportFile, new Wrapper());
274205
$this->assertEquals(
275206
0,
276-
$result,
277-
"PHP Code Sniffer has found {$result} error(s): " . PHP_EOL . $output
207+
$result = $codeSniffer->run($this->getFullWhitelist()),
208+
"PHP Code Sniffer detected {$result} violation(s): " . PHP_EOL . file_get_contents($reportFile)
278209
);
279210
}
280211

281-
/**
282-
* Run mess detector on code
283-
*
284-
* @return void
285-
*/
286-
public function testCodeMess()
212+
public function testNoViolationsDetectedByPhpMessDetector()
287213
{
288214
$reportFile = self::$reportDir . '/phpmd_report.txt';
289215
$codeMessDetector = new CodeMessDetector(realpath(__DIR__ . '/_files/phpmd/ruleset.xml'), $reportFile);
@@ -312,12 +238,7 @@ public function testCodeMess()
312238
}
313239
}
314240

315-
/**
316-
* Run copy paste detector on code
317-
*
318-
* @return void
319-
*/
320-
public function testCopyPaste()
241+
public function testNoViolationsDetectedByPhpCopyPasteDetector()
321242
{
322243
$reportFile = self::$reportDir . '/phpcpd_report.xml';
323244
$copyPasteDetector = new CopyPasteDetector($reportFile);

dev/tests/static/testsuite/Magento/Test/Php/_files/phpcs/ruleset.xml

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
module * /
2-
library * /
3-
dev/tools/Magento
4-
dev/tests/api-functional
5-
dev/tests/functional
6-
dev/tests/integration
7-
dev/tests/static
8-
setup
1+
# Format: <componentType=module|library|theme|language|*> <componentName> <globPattern> or simply <globPattern>
2+
* * /
3+
dev
4+
phpserver
5+
pub
6+
setup
7+
*.php

0 commit comments

Comments
 (0)