Skip to content

Commit 476b29b

Browse files
Add M2 mess detector action
1 parent 3b35486 commit 476b29b

6 files changed

Lines changed: 206 additions & 0 deletions

File tree

magento-mess-detector/Dockerfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM duhon/php:7.3-alpine AS builder
2+
3+
RUN composer create-project --repository=https://repo-magento-mirror.fooman.co.nz/ magento/project-community-edition /var/www/magento2ce --no-install --no-interaction
4+
RUN composer config --unset repo.0
5+
RUN composer config repo.foomanmirror composer https://repo-magento-mirror.fooman.co.nz/
6+
RUN composer install --prefer-dist
7+
8+
9+
FROM php:7.3-cli-alpine3.9
10+
11+
RUN curl -L https://phar.phpunit.de/phpunit-6.5.9.phar -o /usr/local/bin/phpunit
12+
RUN chmod +x /usr/local/bin/phpunit
13+
COPY --from=builder /var/www/magento2ce/ /m2/
14+
RUN echo memory_limit = -1 >> /usr/local/etc/php/conf.d/custom-memory.ini
15+
ADD phpunit.phpmd.xml /m2/dev/tests/static/phpunit.phpmd.xml
16+
ADD PhpmdRunner.php /m2/dev/tests/static/testsuite/Magento/Test/Php/PhpmdRunner.php
17+
ADD LiveCodePhpmdRunner.php /m2/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/LiveCodePhpmdRunner.php
18+
19+
ADD entrypoint.sh /entrypoint.sh
20+
ENTRYPOINT ["/entrypoint.sh"]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/**
8+
* PHP Code Mess v1.3.3 tool wrapper
9+
*/
10+
namespace Magento\TestFramework\CodingStandard\Tool;
11+
12+
use \Magento\TestFramework\CodingStandard\ToolInterface;
13+
14+
class LiveCodePhpmdRunner implements ToolInterface
15+
{
16+
/**
17+
* Ruleset directory
18+
*
19+
* @var string
20+
*/
21+
private $rulesetFile;
22+
23+
/**
24+
* Report file
25+
*
26+
* @var string
27+
*/
28+
private $reportFile;
29+
30+
/**
31+
* Constructor
32+
*
33+
* @param string $rulesetDir \Directory that locates the inspection rules
34+
* @param string $reportFile Destination file to write inspection report to
35+
*/
36+
public function __construct($rulesetFile, $reportFile)
37+
{
38+
$this->reportFile = $reportFile;
39+
$this->rulesetFile = $rulesetFile;
40+
}
41+
42+
/**
43+
* Whether the tool can be ran on the current environment
44+
*
45+
* @return bool
46+
*/
47+
public function canRun()
48+
{
49+
return class_exists(\PHPMD\TextUI\Command::class);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function run(array $whiteList)
56+
{
57+
$commandLineArguments = [
58+
'run_file_mock', //emulate script name in console arguments
59+
$this->getSourceCodePath($whiteList),
60+
'text', //report format
61+
$this->rulesetFile,
62+
'--reportfile',
63+
$this->reportFile,
64+
'--suffixes',
65+
'php' ,
66+
'--exclude',
67+
'vendor/,tmp/,var/,generated/,.git/,.idea/'
68+
];
69+
70+
$options = new \PHPMD\TextUI\CommandLineOptions($commandLineArguments);
71+
72+
$command = new \PHPMD\TextUI\Command();
73+
74+
return $command->run($options, new \PHPMD\RuleSetFactory());
75+
}
76+
77+
private function getSourceCodePath($whiteList): string
78+
{
79+
if(!empty($whiteList)){
80+
return implode(',', $whiteList);
81+
}
82+
return defined('PATH_TO_SOURCE')? PATH_TO_SOURCE : '/var/www/html';
83+
}
84+
}
85+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Test\Php;
9+
10+
use Magento\Framework\App\Utility\Files;
11+
use Magento\TestFramework\CodingStandard\Tool\LiveCodePhpmdRunner;
12+
use PHPMD\TextUI\Command;
13+
14+
/**
15+
* Set of tests for static code analysis, e.g. code style, code complexity, copy paste detecting, etc.
16+
*/
17+
class PhpmdRunner extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var string
21+
*/
22+
protected static $reportDir = '';
23+
24+
/**
25+
* @var string
26+
*/
27+
protected static $pathToSource = '';
28+
29+
/**
30+
* Setup basics for all tests
31+
*
32+
* @return void
33+
*/
34+
public static function setUpBeforeClass()
35+
{
36+
self::$pathToSource = BP;
37+
self::$reportDir = self::$pathToSource . '/dev/tests/static/report';
38+
if (!is_dir(self::$reportDir)) {
39+
mkdir(self::$reportDir);
40+
}
41+
}
42+
43+
/**
44+
* Test code quality using phpmd
45+
*/
46+
public function testCodeMess()
47+
{
48+
$reportFile = self::$reportDir . '/phpmd_report.txt';
49+
$codeMessDetector = new LiveCodePhpmdRunner(realpath(__DIR__ . '/_files/phpmd/ruleset.xml'), $reportFile);
50+
51+
if (!$codeMessDetector->canRun()) {
52+
$this->markTestSkipped('PHP Mess Detector is not available.');
53+
}
54+
55+
$result = $codeMessDetector->run([]);
56+
57+
$output = "";
58+
if (file_exists($reportFile)) {
59+
$output = file_get_contents($reportFile);
60+
}
61+
62+
$this->assertEquals(
63+
Command::EXIT_SUCCESS,
64+
$result,
65+
"PHP Code Mess has found error(s):" . PHP_EOL . $output
66+
);
67+
68+
// delete empty reports
69+
if (file_exists($reportFile)) {
70+
unlink($reportFile);
71+
}
72+
}
73+
74+
}
75+

magento-mess-detector/action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: 'Magento Mess Detector'
2+
author: 'ExtDN'
3+
description: 'performs php static code analysis with the Magento Mess Detector ruleset'
4+
runs:
5+
using: 'docker'
6+
image: 'Dockerfile'
7+
branding:
8+
icon: 'code'
9+
color: 'green'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh -l
2+
export PATH_TO_SOURCE=$GITHUB_WORKSPACE
3+
sh -c "cd /m2/dev/tests/static && /m2/vendor/bin/phpunit -c /m2/dev/tests/static/phpunit.phpmd.xml $*"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.2/phpunit.xsd"
4+
colors="true"
5+
columns="max"
6+
beStrictAboutTestsThatDoNotTestAnything="false"
7+
bootstrap="./framework/bootstrap.php"
8+
>
9+
<testsuites>
10+
<testsuite name="phpmd-m2">
11+
<file>testsuite/Magento/Test/Php/PhpmdRunner.php</file>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

0 commit comments

Comments
 (0)