Converter   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 3
dl 0
loc 105
c 0
b 0
f 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMarkdown() 0 4 1
A __construct() 0 7 1
A parserFromFile() 0 6 1
A normalizeFullName() 0 4 1
A parseParent() 0 11 3
A parseItens() 0 19 2
A parseMethods() 0 10 2
A parseArgument() 0 19 4
A parserMethod() 0 10 1
1
<?php
2
3
/*
4
 * This file is part of gpupo/pipe2
5
 *
6
 * (c) Gilmar Pupo <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * For more information, see
12
 * <https://opensource.gpupo.com/pipe2/>.
13
 */
14
15
namespace Gpupo\Pipe2\Documentor;
16
17
use Gpupo\Common\Entity\Collection;
18
use Gpupo\Pipe2\Traits\DocumentContainerTrait;
19
use SimpleXmlElement;
20
21
class Converter
22
{
23
    use DocumentContainerTrait;
24
25
    protected $list;
26
27
    public function getMarkdown()
28
    {
29
        return $this->getDocument()->render($this->list);
30
    }
31
32
    public function __construct(Array $parameters)
33
    {
34
        $this->setDocument(new Document());
35
        $this->list = $this->parserFromFile($parameters['inputFile']);
36
37
        file_put_contents($parameters['outputFile'], $this->getMarkdown());
38
    }
39
40
    protected function parserFromFile($file)
41
    {
42
        $xml = simplexml_load_file($file);
43
44
        return $this->parseItens($xml);
45
    }
46
47
    protected function normalizeFullName($fullName)
48
    {
49
        return ltrim((string) $fullName, '\\');
50
    }
51
52
    protected function parseParent($raw)
53
    {
54
        $list = [];
55
        if (!empty($raw)) {
56
            foreach ($raw as $parent) {
57
                $list[] = ltrim((string) $parent, '\\');
58
            }
59
        }
60
61
        return $list;
62
    }
63
64
    protected function parseItens(SimpleXmlElement $xml)
65
    {
66
        $list = [];
67
        foreach ($xml->xpath('file/class|file/interface') as $class) {
68
            $name = $this->normalizeFullName($class->full_name);
69
            $list[$name] = [
70
                'className'       => $name,
71
                'shortClass'      => $class->name,
72
                'namespace'       => $class['namespace'],
73
                'description'     => $class->docblock->description,
74
                'longDescription' => $class->docblock->{'long-description'},
75
                'implements'      => $this->parseParent($class->implements),
76
                'extends'         => $this->parseParent($class->extends),
77
                'methods'         => $this->parseMethods($class),
78
            ];
79
        }
80
81
        return new Collection($list);
82
    }
83
84
    protected function parseMethods(SimpleXMLElement $class)
85
    {
86
        $list = [];
87
88
        foreach ($class->method as $method) {
89
            $list[] = $this->parserMethod($method);
90
        }
91
92
        return $list;
93
    }
94
95
    protected function parseArgument($method)
96
    {
97
        $list = [];
98
        foreach ($method->argument as $argument) {
99
            $data = get_object_vars($argument);
100
101
            if ($data['type'] && is_object($data['type'])) {
102
                $data['type'] = get_object_vars($data[$type]);
0 ignored issues
show
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
103
            }
104
105
            $filter = 'docblock/tag[@name="param" and @variable="'.$data['name'].'"]';
106
107
            $tag = $method->xpath($filter);
0 ignored issues
show
Unused Code introduced by
$tag is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
109
            $list[] = $data;
110
        }
111
112
        return new Collection($list);
113
    }
114
115
    protected function parserMethod($method)
116
    {
117
        return new Collection([
118
            'name'        => $method->name,
119
            'description' => (string) $method->docblock->description."\n\n".(string) $method->docblock->{'long-description'},
120
            'visibility'  => (string) $method['visibility'],
121
            'static'      => ((string) $method['static']) === 'true',
122
            'arguments'   => $this->parseArgument($method),
123
        ]);
124
    }
125
}
126