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