Completed
Push — image-processors-refactoring ( dcd98f )
by Luis
05:57
created

plPhuml   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 43.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 62
ccs 13
cts 30
cp 0.4333
rs 10
c 1
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
B addProcessor() 0 10 5
A __construct() 0 5 1
B addDirectory() 0 11 5
A generate() 0 13 2
1
<?php
2
3
use PhUml\Parser\TokenParser;
4
use PhUml\Processors\InvalidInitialProcessor;
5
use PhUml\Processors\InvalidProcessorChain;
6
7
class plPhuml
8
{
9
    /** @var TokenParser */
10
    public $generator;
11
12
    /** @var string[] */
13
    private $files;
14
15
    /** @var plProcessor[] */
16
    private $processors;
17
18 18
    public function __construct()
19
    {
20 18
        $this->generator = new TokenParser();
21 18
        $this->processors = [];
22 18
        $this->files = [];
23 18
    }
24
25
    public function addDirectory(string $directory, string $extension = 'php', bool $recursive = true)
26
    {
27
        if (!$recursive) {
28
            $iterator = new DirectoryIterator($directory);
29
        } else {
30
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
0 ignored issues
show
Bug introduced by
The call to RecursiveDirectoryIterator::__construct() has too few arguments starting with flags. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
            $iterator = new RecursiveIteratorIterator(/** @scrutinizer ignore-call */ new RecursiveDirectoryIterator($directory));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
31
        }
32
33
        foreach ($iterator as $entry) {
34
            if (!$entry->isDir() && $entry->getExtension() === $extension) {
35
                $this->files[] = $entry->getPathname();
36
            }
37
        }
38
    }
39
40
    /**
41
     * @throws InvalidInitialProcessor
42
     * @throws InvalidProcessorChain
43
     */
44 18
    public function addProcessor(plProcessor $processor): void
45
    {
46 18
        if (count($this->processors) === 0 && !$processor->isInitial()) {
47 6
            throw InvalidInitialProcessor::given($processor);
48
        }
49 12
        $lastProcessor = end($this->processors);
50 12
        if (count($this->processors) > 0 && !$lastProcessor->isCompatibleWith($processor)) {
51 12
            throw InvalidProcessorChain::with($lastProcessor, $processor);
52
        }
53 12
        $this->processors[] = $processor;
54 12
    }
55
56
    public function generate($outfile)
57
    {
58
        echo "[|] Parsing class structure\n";
59
        $structure = $this->generator->createStructure($this->files);
60
61
        $input = $structure;
62
        foreach ($this->processors as $processor) {
63
            echo "[|] Running '{$processor->name()}' processor\n";
64
            $input = $processor->process($input);
65
        }
66
67
        echo "[|] Writing generated data to disk\n";
68
        end($this->processors)->writeToDisk($input, $outfile);
69
    }
70
}
71