Completed
Push — master ( 9b53e2...2758ff )
by Andrea
19:08 queued 08:29
created

ChecksrcCommand::runcmd()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 12
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 3
        $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
    protected function execute(InputInterface $input, OutputInterface $output)
22
    {
23
        $container = $this->getContainer();
24
        $prjpath = $container->get("pannelloamministrazione.projectpath");
25
        $vendorBin = $prjpath->getVendorBinPath() . "/";
26
        $srcPath = $prjpath->getSrcPath();
27
        $rootPath = $prjpath->getRootPath();
28
29
        /* phpcpd */
30
        $phpcpdcmd = $vendorBin . "phpcpd " . $srcPath;
31
        $phpcpdoutput = $this->runcmd($phpcpdcmd);
32
        if (!$phpcpdoutput) {
33
            $output->writeln("phpmd: OK");
34
        } else {
35
            if (strpos($phpcpdoutput, "0.00%")) {
36
                $output->writeln("phpmd: OK");
37
            } else {
38
                $output->writeln($phpcpdoutput);
39
            }
40
        }
41
        /* phpcpd */
42
43
        /* phpcs */
44
        $phpcscmd = $vendorBin . "phpcs --standard=" . $rootPath . "/tools/phpcs/ruleset.xml  --extensions=php " . $srcPath;
45
        $phpcsoutput = $this->runcmd($phpcscmd);
46
        if (!$phpcsoutput) {
47
            $output->writeln("phpcs: OK");
48
        } else {
49
            $output->writeln($phpcsoutput);
50
            $output->writeln("Per correggere automaticamente un file eseguire:");
51
            $output->writeln($vendorBin . "phpcbf --standard=PSR2 nomefile.php");
52
        }
53
        /* phpcs */
54
55
        /* phpmd */
56
        $phpmdcmd = $vendorBin . "phpmd " . $srcPath . " text " . $rootPath . "/tools/phpmd/ruleset.xml";
57
        $phpmdoutput = $this->runcmd($phpmdcmd);
58
        if (!$phpmdoutput) {
59
            $output->writeln("phpmd: OK");
60
        } else {
61
            $output->writeln($phpmdoutput);
62
        }
63
        /* phpmd */
64
    }
65
66
    private function runcmd($cmd)
67
    {
68
        $process = new Process($cmd);
69
        $process->setTimeout(60 * 100);
70
        $process->run();
71
        if ($process->isSuccessful()) {
72
            $out = $process->getOutput();
73
        } else {
74
            $out = ($process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput());
75
        }
76
        return $out;
77
    }
78
}
79