1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\PannelloAmministrazioneBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
class ChecksrcCommand extends ContainerAwareCommand |
11
|
|
|
{ |
12
|
|
|
|
13
|
3 |
|
protected function configure() |
14
|
|
|
{ |
15
|
|
|
$this |
16
|
3 |
|
->setName('pannelloamministrazione:checksrc') |
17
|
3 |
|
->setDescription('Controlla i sorgenti') |
18
|
3 |
|
->setHelp('Usa phpcs, phpmd, ecc per controllare il codice in src'); |
19
|
3 |
|
} |
20
|
|
|
|
21
|
|
|
// @codeCoverageIgnoreStart |
22
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
23
|
|
|
{ |
24
|
|
|
$container = $this->getContainer(); |
25
|
|
|
$prjpath = $container->get("pannelloamministrazione.projectpath"); |
|
|
|
|
26
|
|
|
$vendorBin = $prjpath->getVendorBinPath() . "/"; |
|
|
|
|
27
|
|
|
$srcPath = $prjpath->getSrcPath(); |
|
|
|
|
28
|
|
|
$rootPath = $prjpath->getRootPath(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
/* phpcpd */ |
31
|
|
|
$phpcpdcmd = $vendorBin . "phpcpd " . $srcPath; |
|
|
|
|
32
|
|
|
$phpcpdoutput = $this->runcmd($phpcpdcmd); |
33
|
|
|
if (!$phpcpdoutput) { |
34
|
|
|
$output->writeln("phpmd: OK"); |
|
|
|
|
35
|
|
|
} else { |
36
|
|
|
if (strpos($phpcpdoutput, "0.00%")) { |
|
|
|
|
37
|
|
|
$output->writeln("phpmd: OK"); |
|
|
|
|
38
|
|
|
} else { |
39
|
|
|
$output->writeln($phpcpdoutput); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
/* phpcpd */ |
43
|
|
|
|
44
|
|
|
/* phpcs */ |
45
|
|
|
$phpcscmd = $vendorBin . "phpcs --standard=" . $rootPath . "/tools/phpcs/ruleset.xml --extensions=php " . $srcPath; |
|
|
|
|
46
|
|
|
$phpcsoutput = $this->runcmd($phpcscmd); |
47
|
|
|
if (!$phpcsoutput) { |
48
|
|
|
$output->writeln("phpcs: OK"); |
|
|
|
|
49
|
|
|
} else { |
50
|
|
|
$output->writeln($phpcsoutput); |
51
|
|
|
$output->writeln("Per correggere automaticamente un file eseguire:"); |
|
|
|
|
52
|
|
|
$output->writeln($vendorBin . "phpcbf --standard=PSR2 nomefile.php"); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
/* phpcs */ |
55
|
|
|
|
56
|
|
|
/* phpmd */ |
57
|
|
|
$phpmdcmd = $vendorBin . "phpmd " . $srcPath . " text " . $rootPath . "/tools/phpmd/ruleset.xml"; |
|
|
|
|
58
|
|
|
$phpmdoutput = $this->runcmd($phpmdcmd); |
59
|
|
|
if (!$phpmdoutput) { |
60
|
|
|
$output->writeln("phpmd: OK"); |
|
|
|
|
61
|
|
|
} else { |
62
|
|
|
$output->writeln($phpmdoutput); |
63
|
|
|
} |
64
|
|
|
/* phpmd */ |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function runcmd($cmd) |
68
|
|
|
{ |
69
|
|
|
$process = new Process($cmd); |
70
|
|
|
$process->setTimeout(60 * 100); |
71
|
|
|
$process->run(); |
72
|
|
|
if ($process->isSuccessful()) { |
73
|
|
|
$out = $process->getOutput(); |
74
|
|
|
} else { |
75
|
|
|
$out = ($process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput()); |
76
|
|
|
} |
77
|
|
|
return $out; |
78
|
|
|
} |
79
|
|
|
// @codeCoverageIgnoreEnd |
80
|
|
|
} |
81
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.