Failed Conditions
Push — future/ConfuenceWrapper ( 07e591...c4d11e )
by
unknown
08:15
created

Parser::xmlToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * Created by IntelliJ IDEA.
4
 * User: yoshi
5
 * Date: 26.01.16
6
 * Time: 12:42
7
 */
8
9
namespace CodeMine\ConfluenceImporter\Parser;
10
use CodeMine\ConfluenceImporter\Parser\File\File;
11
use CodeMine\ConfluenceImporter\Parser\Structure\Attribute\Constant;
12
use CodeMine\ConfluenceImporter\Parser\Structure\Attribute\Property;
13
14
/**
15
 * Class Parser
16
 * @package CodeMine\ConfluenceImporter\Parser
17
 */
18
class Parser
19
{
20
    private $xmlString;
21
22
    private $classArray;
23
    private $package;
24
    private $namespace;
25
    private $deprecated;
26
27
    /**
28
     * Parser constructor.
29
     * @param string $path
30
     */
31
    public function __construct($path = 'C:\Users\YoSHi\IdeaProjects\PhpDoc-Confluence-Importer\doc\structure.xml')
32
    {
33
        $this->xmlString = file_get_contents($path);
34
35
        $array = $this->xmlToArray($this->xmlString);
36
37
        $this->classArray = $array['file'];
38
        $this->package    = $array['package'];
39
        $this->namespace  = $array['namespace'];
40
        $this->deprecated = $array['deprecated'];
41
42
        $this->prepareFiles();
43
//        print_r($this->fileArray);die;
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
    }
45
46
47
    private function prepareFiles()
48
    {
49
        $fileCollection = new \SplObjectStorage();
50
51
        foreach ($this->classArray as $class){
52
53
            $file = new File();
54
55
            if (isset($class['@attributes'])){
56
                $path = $class['@attributes']['path'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
57
                $package = $class['@attributes']['package'];
58
                $file->setPath($path);
59
                $file->setPackage($package);
60
            }
61
62
            foreach ($class as $key => $value){
63
                switch($key){
64
                    case 'class':
65
                        $file->setType($key);
66
                        $file->setAbstract(strtolower($class['class']['@attributes']['abstract']) === 'true');
67
                        $file->setFinal(strtolower($class['class']['@attributes']['final']) === 'true');
68
                        $file->setNamespace($class['class']['@attributes']['namespace']);
69
                        $file->setExtends(is_array($class['class']['extends']) ? NULL : $class['class']['extends']);
70
71
                        if (isset($class['class']['implements'])) {
72
                            $file->setImplements($class['class']['implements']);
73
74
                            ////        var_dump($array['file'][0]['class']['constant'][0]['name']);
75
////        var_dump($array['file'][0]['class']['constant'][0]['full_name']);
76
////        var_dump($array['file'][0]['class']['constant'][0]['value']);
77
78
79
                        }
80
81
                        if (isset($class['namespace-alias'])){
82
                            $file->setNamespaceAlias($class['namespace-alias']);
83
                        }
84
85
//                        var_dump($class);die;
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
//                        var_dump((isset($class['constant'])));
87
                        if (isset($class['class']['constant'])){
88
                            $constant = new Constant();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
89
                            $name = $class['class']['constant']['name'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
90
                            $fullName = $class['class']['constant']['full_name'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
91
                            $valueType = $class['class']['constant']['docblock']['tag']['@attributes']['type'];
92
                            $value = $class['class']['constant']['value'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
93
                            $constant->setName($name);
94
                            $constant->setFullName($fullName);
95
                            $constant->setValueType($valueType);
96
                            $constant->setValue($value);
97
                            $file->setConstant($constant);
98
                        }
99
100
                        if (isset($class['class']['property'])){
101
                            $test = $class['class']['property'];
0 ignored issues
show
Unused Code introduced by
$test 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...
102
                            $this->setProperty($class['class']['property'], $file);
103
                        }
104
105
106
                        echo $key . PHP_EOL;
107
                        break;
108
                    case 'interface':
109
                        $file->setType($key);
110
                        $file->setNamespace($class['interface']['@attributes']['namespace']);
111
                        if (isset($class['namespace-alias'])){
112
                            $file->setNamespaceAlias($class['namespace-alias']);
113
                        }
114
115
                        echo $key . PHP_EOL;
116
                        break;
117
                    case 'trait':
118
                        $file->setType($key);
119
                        if (isset($class['namespace-alias'])){
120
                            $file->setNamespaceAlias($class['namespace-alias']);
121
                        }
122
                        echo $key . PHP_EOL;
123
                        break;
124
                }
125
126
            }
127
128
            $fileCollection->attach($file);
129
130
131
        }
132
133
        return $fileCollection;
134
135
    }
136
137
138
139
140
    /**
141
     * @param $xmlString
142
     * @return string
143
     */
144
    public function xmlToJson($xmlString)
145
    {
146
        $json = $this->generateJson($xmlString);
147
148
        return $json;
149
    }
150
151
    /**
152
     * @param string $xmlString
153
     * @return array
154
     */
155
    public function xmlToArray($xmlString)
156
    {
157
        $json = $this->generateJson($xmlString);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
158
        $array = json_decode($json, true);
159
160
        return $array;
161
162
    }
163
164
    /**
165
     * @param $xmlString
166
     * @return string
167
     */
168
    private function generateJson($xmlString)
169
    {
170
        $xml  = simplexml_load_string($xmlString);
171
        $json = json_encode($xml);
172
173
        return $json;
174
    }
175
176
    private function setProperty(array $property, File $file)
177
    {
178
//        print_r($property);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
179
        $zal = array_key_exists('name', $property);
0 ignored issues
show
Unused Code introduced by
$zal 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...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
180
        $zalozenie = (FALSE === array_key_exists('name', $property));
0 ignored issues
show
Unused Code introduced by
$zalozenie 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...
181
        if(FALSE === array_key_exists('name', $property)){
182
            foreach ($property as $prop){
183
                $this->setProperty($prop, $file);
184
            }
185
            return;
186
        }
187
        $propertyToAdd = new Property();
188
        $propertyToAdd->setName($property['name']);
189
190
        $file->addProperty($propertyToAdd);
191
    }
192
}