Completed
Push — master ( 15a86c...ff0242 )
by Michael
03:23
created

xmlparser::normalize()   F

Complexity

Conditions 28
Paths 3200

Size

Total Lines 85
Code Lines 54

Duplication

Lines 6
Ratio 7.06 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 54
c 1
b 0
f 0
nc 3200
nop 0
dl 6
loc 85
rs 2.1751

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
// ------------------------------------------------------------------------ //
4
// This program is free software; you can redistribute it and/or modify     //
5
// it under the terms of the GNU General Public License as published by     //
6
// the Free Software Foundation; either version 2 of the License, or        //
7
// (at your option) any later version.                                      //
8
//                                                                          //
9
// You may not change or alter any portion of this comment or credits       //
10
// of supporting developers from this source code or any supporting         //
11
// source code which is considered copyrighted (c) material of the          //
12
// original comment or credit authors.                                      //
13
//                                                                          //
14
// This program is distributed in the hope that it will be useful,          //
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
// GNU General Public License for more details.                             //
18
//                                                                          //
19
// You should have received a copy of the GNU General Public License        //
20
// along with this program; if not, write to the Free Software              //
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------ //
23
// Author: phppp (D.J., [email protected])                                  //
24
// URL: http://xoops.org                         //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
/**
28
 * @package   module::blogline
29
 * @copyright copyright &copy; 2005 XoopsForge.com
30
 */
31
global $msg;
32
33
require_once XOOPS_ROOT_PATH . '/modules/' . $GLOBALS['moddirname'] . '/class/magpie.inc.php';
34
35
/**
36
 * XmlParser
37
 *
38
 * @author    D.J. (phppp)
39
 * @copyright copyright &copy; 2005 XoopsForge.com
40
 * @package   module::article
41
 *
42
 * {@link MagpieRSS}
43
 **/
44
class xmlparser extends MagpieRSS
45
{
46
    public $content;
47
    public $charset_in;
48
    public $charset_out;
49
50
    /**
51
     *  Set up XML parser, parse source, and return populated RSS object..
52
     *
53
     * @param string      $content    string containing the RSS to be parsed
54
     *
55
     *
56
     * @param string      $input_charset
57
     * @param null|string $output_charset
58
     * @param array       $tags
59
     * @internal param string $output_encoding output the parsed RSS in this character
60
     *                                set defaults to ISO-8859-1 as this is PHP's
61
     *                                default.
62
     *
63
     * @internal param string $input_encoding the character set of the incoming RSS source.
64
     *                                Leave blank and Magpie will try to figure it
65
     *                                out.
66
     */
67
    public function __construct($content, $input_charset, $output_charset = _CHARSET, $tags = array()) {
68
        if (!in_array(strtoupper($input_charset), array('UTF-8', 'US-ASCII', 'ISO-8859-1'))) {
69
            $content       = XoopsLocal::convert_encoding($content, 'UTF-8', $input_charset);
70
            $content       = preg_replace('/(<\?xml.*encoding=[\'"])(.*?)([\'"].*\?>)/m', '$1UTF-8$3', $content);
71
            $input_charset = 'UTF-8';
72
        }
73
        $this->content     = $content;
74
        $this->charset_in  = $input_charset;
75
        $this->charset_out = $output_charset;
76
77
        /* TODO: parse specified tags only */
78
        parent::__construct($content, $input_charset, $input_charset, false);
79
80
        //xoops_message($this);
81
        unset($this->content);
82
        $this->encoding_convert($tags);
83
    }
84
85
    /**
86
     * @return bool|string
87
     */
88
    public function is_atom() {
89
        if ($this->feed_type == ATOM) {
90
            $this->feed_version = empty($this->feed_version) ? '0.3' : $this->feed_version;
91
92
            return $this->feed_version;
93
        } else {
94
            return false;
95
        }
96
    }
97
98
    public function normalize() {
99
        if ($this->is_atom()):
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->is_atom() of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
100
            if (empty($this->channel['tagline'])) {
101
                /* ATOM */
102
                $this->channel['tagline'] = @$this->channel['subtitle'];
103
                unset($this->channel['subtitle']);
104
            }
105
            for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) {
106
                // ATOM time
107
                if ($date = @$this->items[$i]['modified']) {
108
                    continue;
109
                }
110
                if (empty($date)) {
111
                    $date = @$this->items[$i]['updated'];
112
                }
113
                if (empty($date)) {
114
                    $date = @$this->items[$i]['issued'];
115
                }
116
                if (empty($date)) {
117
                    $date = @$this->items[$i]['created'];
118
                }
119
                if (empty($date)) {
120
                    $date = @$this->items[$i]['created'];
121
                }
122
                $this->items[$i]['modified'] = $date;
123
            }
124
        elseif ($this->is_rss() !== '1.0'):
125
            for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) {
126
                if ($date = @$this->items[$i]['pubdate']) {
0 ignored issues
show
Unused Code introduced by
$date 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...
127
                    continue;
128
                }
129
                $this->items[$i]['pubdate'] = @$this->items[$i]['dc']['date'];
130
            }
131
        endif;
132
        parent::normalize();
133
        /* ATOM */
134
        if (empty($this->channel['language']) && !empty($this->channel['dc']['language'])) {
135
            $this->channel['language'] = $this->channel['dc']['language'];
136
            unset($this->channel['dc']['language']);
137
        }
138 View Code Duplication
        if (empty($this->channel['language'])
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...
139
            && preg_match('/<link.*hreflang=[\'"](.*?)[\'"].*?>/m', $this->content, $match)
140
        ) {
141
            $this->channel['language'] = $match[1];
142
        }
143 View Code Duplication
        if (empty($this->channel['language'])
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...
144
            && preg_match('/<feed.*xml:lang=[\'"](.*?)[\'"].*?>/m', $this->content, $match)
145
        ) {
146
            $this->channel['language'] = $match[1];
147
        }
148
        /* remove to avoid redundant encoding conversion */
149
        if (!empty($this->channel['tagline'])) {
150
            unset($this->channel['tagline']);
151
        }
152
153
        for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) {
154
            if ($date_timestamp = @$this->items[$i]['date_timestamp']) {
0 ignored issues
show
Unused Code introduced by
$date_timestamp 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...
155
                continue;
156
            }
157
            if ($date_timestamp = @$this->items[$i]['pubdate']) {
158
                $this->items[$i]['date_timestamp'] = $date_timestamp;
159
            } elseif ($date_timestamp = @$this->items[$i]['dc']['date']) {
160
                $this->items[$i]['date_timestamp'] = $date_timestamp;
161
            } else {
162
                $this->items[$i]['date_timestamp'] = time();
163
            }
164
            if (!is_numeric($this->items[$i]['date_timestamp'])) {
165
                if ($date = parse_w3cdtf($this->items[$i]['date_timestamp'])) {
166
                    $this->items[$i]['date_timestamp'] = $date;
167
                } elseif ($date = strtotime($this->items[$i]['date_timestamp'])) {
168
                    $this->items[$i]['date_timestamp'] = $date;
169
                }
170
            }
171
172
            /* remove to avoid redundant encoding conversion */
173
            if (isset($this->items[$i]['summary'])) {
174
                unset($this->items[$i]['summary']);
175
            }
176
            if (isset($this->items[$i]['atom_content'])) {
177
                unset($this->items[$i]['atom_content']);
178
            }
179
        }
180
181
        return;
182
    }
183
184
    /**
185
     * @param array $tags
186
     */
187
    public function encoding_convert($tags = array()) {
188
        if (empty($tags) || in_array('channel', $tags)) {
189
            $this->channel = $this->_encoding($this->channel);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_encoding($this->channel) of type * is incompatible with the declared type array of property $channel.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
190
        }
191
        if (empty($tags) || in_array('items', $tags)) {
192
            $this->items = $this->_encoding($this->items);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_encoding($this->items) of type * is incompatible with the declared type array of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
193
        }
194
    }
195
196
    /**
197
     * @param $val
198
     * @return array|mixed|string
199
     */
200 View Code Duplication
    public function _encoding($val) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
201
        if (is_array($val)) {
202
            foreach (array_keys($val) as $key) {
203
                $val[$key] = $this->_encoding($val[$key]);
204
            }
205
        } else {
206
            $val = XoopsLocal::convert_encoding($val, $this->charset_out, $this->charset_in);
207
        }
208
209
        return $val;
210
    }
211
}
212