Skip to content

Commit 9cb5403

Browse files
Start work on a phpstan action
1 parent 24235b0 commit 9cb5403

9 files changed

Lines changed: 229 additions & 5 deletions

File tree

.github/workflows/docker.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ jobs:
1515
actions-with-docker-image:
1616
- "magento-coding-standard"
1717
- "magento-mess-detector"
18-
18+
- "magento-phpstan"
19+
-
1920
env:
2021
DOCKER_USERNAME: "extdn"
2122

magento-coding-standard/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
name: 'Magento Coding Style'
1+
name: 'Magento 2 Coding Style'
22
author: 'ExtDN'
3-
description: 'performs php static code analysis with the Magento Coding Standard'
3+
description: 'performs php static code analysis with the Magento 2 Coding Standard'
44
runs:
55
using: 'docker'
66
image: 'docker://extdn/magento-coding-standard-action:latest'

magento-mess-detector/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
name: 'Magento Mess Detector'
1+
name: 'Magento 2 Mess Detector'
22
author: 'ExtDN'
3-
description: 'performs php static code analysis with the Magento Mess Detector ruleset'
3+
description: 'performs php static code analysis with the Magento 2 Mess Detector ruleset'
44
runs:
55
using: 'docker'
66
image: 'docker://extdn/magento-mess-detector-action:latest'

magento-phpstan/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
COPY --from=builder /var/www/magento2ce/ /m2/
11+
RUN echo memory_limit = -1 >> /usr/local/etc/php/conf.d/custom-memory.ini
12+
ADD phpunit.phpmd.xml /m2/dev/tests/static/phpunit.phpmd.xml
13+
ADD PhpmdRunner.php /m2/dev/tests/static/testsuite/Magento/Test/Php/PhpStanRunner.php
14+
ADD LiveCodePhpmdRunner.php /m2/dev/tests/static/framework/Magento/TestFramework/CodingStandard/Tool/LiveCodePhpmdRunner.php
15+
16+
ADD entrypoint.sh /entrypoint.sh
17+
ENTRYPOINT ["/entrypoint.sh"]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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\TestFramework\CodingStandard\Tool;
9+
10+
use Magento\TestFramework\CodingStandard\ToolInterface;
11+
12+
/**
13+
* PhpStan tool wrapper.
14+
*/
15+
class LiveCodePhpStanRunner implements ToolInterface
16+
{
17+
/**
18+
* Rule level to be used.
19+
*
20+
* @see https://github.com/phpstan/phpstan#rule-levels
21+
*/
22+
private const RULE_LEVEL = 0;
23+
24+
/**
25+
* Memory limit required by PHPStan for full Magento project scan.
26+
*/
27+
private const MEMORY_LIMIT = '4G';
28+
29+
/**
30+
* Error formatter to be used.
31+
*
32+
* @see https://github.com/phpstan/phpstan#existing-error-formatters-to-be-used
33+
*/
34+
private const ERROR_FORMAT = 'raw';
35+
36+
/**
37+
* Report file.
38+
*
39+
* @var string
40+
*/
41+
private $reportFile;
42+
43+
/**
44+
* PHPStan configuration file in neon format.
45+
*
46+
* @var string
47+
*/
48+
private $confFile;
49+
50+
/**
51+
* @param string $confFile
52+
* @param string $reportFile
53+
*/
54+
public function __construct(string $confFile, string $reportFile)
55+
{
56+
$this->reportFile = $reportFile;
57+
$this->confFile = $confFile;
58+
}
59+
60+
/**
61+
* @inheritdoc
62+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
63+
*/
64+
public function canRun(): bool
65+
{
66+
// phpcs:disable Magento2.Security.InsecureFunction
67+
exec($this->getCommand() . ' --version', $output, $exitCode);
68+
return $exitCode === 0;
69+
}
70+
71+
/**
72+
* @inheritdoc
73+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
74+
*/
75+
public function run(array $whiteList): int
76+
{
77+
78+
$command = $this->getCommand() . ' analyse' .
79+
' --level ' . self::RULE_LEVEL .
80+
' --no-progress' .
81+
' --error-format=' . self::ERROR_FORMAT .
82+
' --memory-limit=' . self::MEMORY_LIMIT .
83+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
84+
' --configuration ' . escapeshellarg($this->confFile) .
85+
' ' . $this->getSourceCodePath($whiteList).
86+
' > ' . $this->reportFile;
87+
88+
// phpcs:disable Magento2.Security.InsecureFunction
89+
exec($command, $output, $exitCode);
90+
91+
return $exitCode;
92+
}
93+
94+
/**
95+
* Get PHPStan CLI command
96+
*
97+
* @return string
98+
*/
99+
private function getCommand(): string
100+
{
101+
// phpcs:ignore Magento2.Security.IncludeFile
102+
$vendorDir = require BP . '/app/etc/vendor_path.php';
103+
return 'php ' . BP . '/' . $vendorDir . '/bin/phpstan';
104+
}
105+
106+
private function getSourceCodePath($whiteList): string
107+
{
108+
if(!empty($whiteList)){
109+
return implode(',', $whiteList);
110+
}
111+
return defined('PATH_TO_SOURCE')? PATH_TO_SOURCE : '/var/www/html';
112+
}
113+
114+
}

magento-phpstan/PhpStanRunner.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\LiveCodePhpStanRunner;
12+
13+
/**
14+
* Set of tests for static code analysis, e.g. code style, code complexity, copy paste detecting, etc.
15+
*/
16+
class PhpStanRunner extends \PHPUnit\Framework\TestCase
17+
{
18+
/**
19+
* @var string
20+
*/
21+
protected static $reportDir = '';
22+
23+
/**
24+
* @var string
25+
*/
26+
protected static $pathToSource = '';
27+
28+
/**
29+
* Setup basics for all tests
30+
*
31+
* @return void
32+
*/
33+
public static function setUpBeforeClass()
34+
{
35+
self::$pathToSource = BP;
36+
self::$reportDir = self::$pathToSource . '/dev/tests/static/report';
37+
if (!is_dir(self::$reportDir)) {
38+
mkdir(self::$reportDir);
39+
}
40+
}
41+
42+
public function testPhpStan()
43+
{
44+
$reportFile = self::$reportDir . '/phpstan_report.txt';
45+
$confFile = __DIR__ . '/_files/phpstan/phpstan.neon';
46+
47+
if (!file_exists($reportFile)) {
48+
touch($reportFile);
49+
}
50+
51+
$phpStan = new LiveCodePhpStanRunner($confFile, $reportFile);
52+
$exitCode = $phpStan->run([]);
53+
$report = file_get_contents($reportFile);
54+
55+
$errorMessage = empty($report) ?
56+
'PHPStan command run failed.' : 'PHPStan detected violation(s):' . PHP_EOL . $report;
57+
$this->assertEquals(0, $exitCode, $errorMessage);
58+
59+
// delete empty reports
60+
if (file_exists($reportFile)) {
61+
unlink($reportFile);
62+
}
63+
}
64+
65+
}
66+

magento-phpstan/action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: 'Magento 2 PhpStan'
2+
author: 'ExtDN'
3+
description: 'performs php static code analysis with the Magento 2 PhpStan ruleset'
4+
runs:
5+
using: 'docker'
6+
image: 'docker://extdn/magento-phpstan-action:latest'
7+
branding:
8+
icon: 'code'
9+
color: 'green'

magento-phpstan/entrypoint.sh

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 $*"

magento-phpstan/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="phpstan-m2">
11+
<file>testsuite/Magento/Test/Php/PhpStanRunner.php</file>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

0 commit comments

Comments
 (0)