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
|
6 |
|
protected function configure() |
14
|
|
|
{ |
15
|
6 |
|
$this |
16
|
6 |
|
->setName('pannelloamministrazione:checksrc') |
17
|
6 |
|
->setDescription('Controlla i sorgenti') |
18
|
6 |
|
->setHelp('Usa phpcs, phpmd, ecc per controllare il codice in src'); |
19
|
6 |
|
} |
20
|
|
|
|
21
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
22
|
|
|
{ |
23
|
1 |
|
$container = $this->getContainer(); |
24
|
1 |
|
$prjpath = $container->get("pannelloamministrazione.projectpath"); |
|
|
|
|
25
|
1 |
|
$vendorBin = $prjpath->getVendorBinPath() . "/"; |
|
|
|
|
26
|
1 |
|
$srcPath = $prjpath->getSrcPath(); |
|
|
|
|
27
|
1 |
|
$rootPath = $prjpath->getRootPath(); |
|
|
|
|
28
|
|
|
|
29
|
|
|
/* phpcpd */ |
30
|
1 |
|
$phpcpdcmd = $vendorBin . "phpcpd " . $srcPath; |
|
|
|
|
31
|
1 |
|
$phpcpdoutput = $this->runcmd($phpcpdcmd); |
32
|
1 |
|
if (!$phpcpdoutput) { |
33
|
|
|
$output->writeln("phpmd: OK"); |
|
|
|
|
34
|
|
|
} else { |
35
|
1 |
|
if (strpos($phpcpdoutput, "0.00%")) { |
|
|
|
|
36
|
|
|
$output->writeln("phpmd: OK"); |
|
|
|
|
37
|
|
|
} else { |
38
|
1 |
|
$output->writeln($phpcpdoutput); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
/* phpcpd */ |
42
|
|
|
|
43
|
|
|
/* phpcs */ |
44
|
1 |
|
$phpcscmd = $vendorBin . "phpcs --standard=" . $rootPath . "/tools/phpcs/ruleset.xml --extensions=php " . $srcPath; |
|
|
|
|
45
|
1 |
|
$phpcsoutput = $this->runcmd($phpcscmd); |
46
|
1 |
|
if (!$phpcsoutput) { |
47
|
|
|
$output->writeln("phpcs: OK"); |
|
|
|
|
48
|
|
|
} else { |
49
|
1 |
|
$output->writeln($phpcsoutput); |
50
|
1 |
|
$output->writeln("Per correggere automaticamente un file eseguire:"); |
|
|
|
|
51
|
1 |
|
$output->writeln($vendorBin . "phpcbf --standard=PSR2 nomefile.php"); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
/* phpcs */ |
54
|
|
|
|
55
|
|
|
/* phpmd */ |
56
|
1 |
|
$phpmdcmd = $vendorBin . "phpmd " . $srcPath . " text " . $rootPath . "/tools/phpmd/ruleset.xml"; |
|
|
|
|
57
|
1 |
|
$phpmdoutput = $this->runcmd($phpmdcmd); |
58
|
1 |
|
if (!$phpmdoutput) { |
59
|
|
|
$output->writeln("phpmd: OK"); |
|
|
|
|
60
|
|
|
} else { |
61
|
1 |
|
$output->writeln($phpmdoutput); |
62
|
|
|
} |
63
|
|
|
/* phpmd */ |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
private function runcmd($cmd) |
67
|
|
|
{ |
68
|
1 |
|
$process = new Process($cmd); |
69
|
1 |
|
$process->setTimeout(60 * 100); |
70
|
1 |
|
$process->run(); |
71
|
1 |
|
if ($process->isSuccessful()) { |
72
|
|
|
$out = $process->getOutput(); |
73
|
|
|
} else { |
74
|
1 |
|
$out = ($process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput()); |
75
|
|
|
} |
76
|
1 |
|
return $out; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.