Completed
Pull Request — master (#26)
by
unknown
03:46
created

Cli::createParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PhpAssumptions;
4
5
use League\CLImate\CLImate;
6
use PhpAssumptions\Output\PrettyOutput;
7
use PhpAssumptions\Output\XmlOutput;
8
use PhpAssumptions\Parser\NodeVisitor;
9
use PhpParser\NodeTraverser;
10
use PhpParser\ParserFactory;
11
12
class Cli
13
{
14
    const VERSION = '0.5.0';
15
16
    /**
17
     * @var CLImate
18
     */
19
    private $cli;
20
21 7
    private function createParser()
22
    {
23 7
        $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
24 7
        return $parser;
25
    }
26
27 7
    public function __construct(CLImate $cli)
28
    {
29 7
        $this->cli = $cli;
30 7
        $this->cli->arguments->add(
31
            [
32 7
            'path' => [
33
                'description' => 'The path to analyse',
34
                'required' => true,
35
            ],
36
            'format' => [
37
                'prefix' => 'f',
38
                'longPrefix' => 'format',
39
                'description' => 'Format (pretty, xml)',
40
                'defaultValue' => 'pretty',
41
            ],
42
            'exclude' => [
43
                'prefix' => 'e',
44
                'longPrefix' => 'exclude',
45
                'description' => 'List of files/directories (separate by ",") to exclude from the analyse',
46
                'defaultValue' => ''
47
            ],
48
            'output' => [
49
                'prefix' => 'o',
50
                'longPrefix' => 'output',
51
                'description' => 'Output file',
52
                'defaultValue' => 'phpa.xml',
53
            ],
54
            ]
55
        );
56 7
        $this->parser = self::createParser();
0 ignored issues
show
Bug introduced by
The property parser does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
57 7
    }
58
59
    /**
60
     * @param array $args
61
     */
62 7
    public function handle(array $args)
63
    {
64 7
        $this->cli->out(sprintf('PHPAssumptions analyser v%s by @rskuipers', Cli::VERSION))->br();
65
66
        try {
67 7
            $this->cli->arguments->parse($args);
68 1
        } catch (\Exception $e) {
69 1
            $this->cli->usage($args);
70 1
            return;
71
        }
72
73 6
        switch ($this->cli->arguments->get('format')) {
74 6
            case 'xml':
75 1
                $output = new XmlOutput($this->cli, $this->cli->arguments->get('output'));
76 1
                break;
77
            default:
78 5
                $output = new PrettyOutput($this->cli);
79 5
                break;
80
        }
81
82 6
        $excludes = $this->getPathsFromList($this->cli->arguments->get('exclude'));
83
84 6
        $nodeTraverser = new NodeTraverser();
85
86 6
        $analyser = new Analyser(
87 6
            $this->parser,
88 6
            $nodeTraverser,
89 6
            $excludes
90
        );
91
92 6
        $nodeTraverser->addVisitor(new NodeVisitor($analyser, new Detector()));
93
94 6
        $target = $this->cli->arguments->get('path');
95 6
        $targets = $this->getPaths($target);
96
97 6
        $result = $analyser->analyse($targets);
98
99 6
        $output->output($result);
100 6
    }
101
102
    /**
103
     * @param string $list
104
     * @return array
105
     */
106 6
    private function getPathsFromList($list)
107
    {
108 6
        $paths = [];
109 6
        if (strlen($list) > 0) {
110 3
            $items = explode(',', $list);
111 3
            foreach ($items as $item) {
112 3
                $paths = array_merge($paths, $this->getPaths($item));
113
            }
114
        }
115
116 6
        return $paths;
117
    }
118
119
    /**
120
     * @param string $fromPath
121
     * @return array
122
     */
123 6
    private function getPaths($fromPath)
124
    {
125 6
        $paths = [];
126 6
        if (is_file($fromPath)) {
127 4
            $paths[] = $fromPath;
128
        } else {
129 3
            $directory = new \RecursiveDirectoryIterator($fromPath);
130 3
            $iterator = new \RecursiveIteratorIterator($directory);
131 3
            $regex = new \RegexIterator($iterator, '/^.+\.php$/i', \RecursiveRegexIterator::GET_MATCH);
132
133 3
            foreach ($regex as $file) {
134 3
                $paths[] = $file[0];
135
            }
136
        }
137
138 6
        return $paths;
139
    }
140
}
141