xmlparser::normalize()   F
last analyzed

Complexity

Conditions 28
Paths 3200

Size

Total Lines 83

Duplication

Lines 6
Ratio 7.23 %

Importance

Changes 0
Metric Value
cc 28
nc 3200
nop 0
dl 6
loc 83
rs 0
c 0
b 0
f 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
// ------------------------------------------------------------------------ //
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: https://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 = [])
68
    {
69
        if (!in_array(strtoupper($input_charset), ['UTF-8', 'US-ASCII', 'ISO-8859-1'])) {
70
            $content       = XoopsLocal::convert_encoding($content, 'UTF-8', $input_charset);
71
            $content       = preg_replace('/(<\?xml.*encoding=[\'"])(.*?)([\'"].*\?>)/m', '$1UTF-8$3', $content);
72
            $input_charset = 'UTF-8';
73
        }
74
        $this->content     = $content;
75
        $this->charset_in  = $input_charset;
76
        $this->charset_out = $output_charset;
77
78
        /* TODO: parse specified tags only */
79
        parent::__construct($content, $input_charset, $input_charset, false);
80
81
        //xoops_message($this);
82
        unset($this->content);
83
        $this->encoding_convert($tags);
84
    }
85
86
    /**
87
     * @return bool|string
88
     */
89
    public function is_atom()
90
    {
91
        if (ATOM == $this->feed_type) {
92
            $this->feed_version = empty($this->feed_version) ? '0.3' : $this->feed_version;
93
94
            return $this->feed_version;
95
        } else {
96
            return false;
97
        }
98
    }
99
100
    public function normalize()
101
    {
102
        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...
103
            if (empty($this->channel['tagline'])) {
104
                /* ATOM */
105
                $this->channel['tagline'] = @$this->channel['subtitle'];
106
                unset($this->channel['subtitle']);
107
            }
108
        for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) {
109
            // ATOM time
110
            if ($date = @$this->items[$i]['modified']) {
111
                continue;
112
            }
113
            if (empty($date)) {
114
                $date = @$this->items[$i]['updated'];
115
            }
116
            if (empty($date)) {
117
                $date = @$this->items[$i]['issued'];
118
            }
119
            if (empty($date)) {
120
                $date = @$this->items[$i]['created'];
121
            }
122
            if (empty($date)) {
123
                $date = @$this->items[$i]['created'];
124
            }
125
            $this->items[$i]['modified'] = $date;
126
        } elseif ('1.0' !== $this->is_rss()):
127
            for ($i = 0, $iMax = count($this->items); $i < $iMax; ++$i) {
128
                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...
129
                    continue;
130
                }
131
                $this->items[$i]['pubdate'] = @$this->items[$i]['dc']['date'];
132
            }
133
        endif;
134
        parent::normalize();
135
        /* ATOM */
136
        if (empty($this->channel['language']) && !empty($this->channel['dc']['language'])) {
137
            $this->channel['language'] = $this->channel['dc']['language'];
138
            unset($this->channel['dc']['language']);
139
        }
140 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...
141
            && preg_match('/<link.*hreflang=[\'"](.*?)[\'"].*?>/m', $this->content, $match)) {
142
            $this->channel['language'] = $match[1];
143
        }
144 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...
145
            && preg_match('/<feed.*xml:lang=[\'"](.*?)[\'"].*?>/m', $this->content, $match)) {
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 = [])
188
    {
189
        if (empty($tags) || in_array('channel', $tags)) {
190
            $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...
191
        }
192
        if (empty($tags) || in_array('items', $tags)) {
193
            $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...
194
        }
195
    }
196
197
    /**
198
     * @param $val
199
     * @return array|mixed|string
200
     */
201 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...
202
    {
203
        if (is_array($val)) {
204
            foreach (array_keys($val) as $key) {
205
                $val[$key] = $this->_encoding($val[$key]);
206
            }
207
        } else {
208
            $val = XoopsLocal::convert_encoding($val, $this->charset_out, $this->charset_in);
209
        }
210
211
        return $val;
212
    }
213
}
214