ParseXml::parse()   D
last analyzed

Complexity

Conditions 15
Paths 514

Size

Total Lines 59
Code Lines 34

Duplication

Lines 16
Ratio 27.12 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 59
rs 4.1106
cc 15
eloc 34
nc 514
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: ignatenkov
5
 * Date: 23.08.15
6
 * Time: 4:51
7
 */
8
9
namespace YaWeather;
10
11
12
13
14
use YaWeather\Models\City;
15
use YaWeather\Models\Day;
16
use YaWeather\Models\Detail;
17
18
class ParseXml {
19
20
    private $xml;
21
    private $city;
0 ignored issues
show
Unused Code introduced by
The property $city is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
23
24
    /**
25
     * @param $fileName
26
     */
27
    public function __construct($fileName)
28
    {
29
        $this->xml = @simplexml_load_file($fileName);
30
    }
31
32
    /**
33
     * Parse weather from XML and return Model City
34
     * @return bool|City
35
     */
36
    public function parse() {
37
38
        // check xml load
39
        if(!$this->xml)
40
            return false;
41
42
        $city = new City();
43 View Code Duplication
        foreach($this->xml->attributes() as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            if(isset(Mapping::mapCity()[$key]))
45
                $city->{Mapping::mapCity()[$key]} = (string) $value;
46
        }
47
48
        $fact = new Detail();
49 View Code Duplication
        foreach ($this->xml->fact[0] as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            if(isset(Mapping::mapDetail()[$key])) {
51
                $fact->{Mapping::mapDetail()[$key]} = (string) $value;
52
            }
53
54
        }
55
        $city->fact = $fact;
56
57
        $fact = new Detail();
58 View Code Duplication
        foreach ($this->xml->yesterday[0] as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            if(isset(Mapping::mapDetail()[$key])) {
60
                $fact->{Mapping::mapDetail()[$key]} = (string) $value;
61
            }
62
63
        }
64
        $city->yesterday = $fact;
65
66
        $days = array();
67
        foreach($this->xml->day as $value) {
68
            $day = new Day();
69
            foreach ($value as $key => $value1) {
70
                if(isset(Mapping::mapDay()[$key])) {
71
                    $day->{Mapping::mapDay()[$key]} = (string) $value1;
72
                }
73
                //var_dump($value1->attributes()["typeid"]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
74
75
76
            }
77
            foreach ($value->day_part as $key2 => $value2) {
78
                $detail = new Detail();
79
                foreach ($value2 as $key3 => $value3) {
80
                    if(isset(Mapping::mapDetail()[$key3])) {
81
                        $detail->{Mapping::mapDetail()[$key3]} = (string) $value3;
82
                    }
83
                    if(isset(Mapping::mapDetail()["inner"][$key3])) {
84
                        $detail->{Mapping::mapDetail()["inner"][$key3]} = (string) $value3->avg;
85
                    }
86
                }
87
                $day->{$value2->attributes()["type"]} = $detail;
88
            }
89
            $days[(string)$value->attributes()["date"]] = $day;
90
        }
91
        $city->days = $days;
92
93
        return $city;
94
    }
95
96
    /**
97
     * static function encode string utf8 to cp1251
98
     * @param $value
99
     * @return string
100
     */
101
    public static function encoding($value) {
102
        return iconv( "UTF-8", "windows-1251//TRANSLIT", $value);
103
    }
104
105
106
107
108
109
110
}