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+ }
0 commit comments