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

ChecksrcCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 13.33%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 69
ccs 6
cts 45
cp 0.1333
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 44 5
A runcmd() 0 12 3
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