GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Meta::__toString()   F
last analyzed

Complexity

Conditions 9
Paths 256

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 24
nc 256
nop 0
dl 0
loc 46
ccs 0
cts 24
cp 0
crap 90
rs 3.1578
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Helpers\View;
4
5
use Nip\Config\Config;
6
use stdClass;
7
8
/**
9
 * Nip Framework
10
 *
11
 * @category   Nip
12
 * @copyright  2009 Nip Framework
13
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
14
 * @version    SVN: $Id: Meta.php 60 2009-04-28 14:50:04Z victor.stanciu $
15
 */
16
class Meta extends AbstractHelper
17
{
18
19
    public $charset = 'utf-8';
20
    public $language = 'en';
21
22
    public $authors = [];
23
    public $publisher;
24
    public $copyright;
25
26
    public $robots = 'index,follow';
27
28
    public $title = false;
29
    public $titleComponents = [
30
        'base' => false,
31
        'elements' => [],
32
        'separator' => ' - ',
33
34
    ];
35
36
    public $keywords = [];
37
    public $description = false;
38
    public $descriptionComponents = [];
39
    public $verify_v1 = false;
40
    public $feeds = [];
41
42
    /**
43
     * @param Config $config
44
     */
45
    public function populateFromConfig($config)
46
    {
47
        $this->setTitleBase($config->get('title'));
48
        $this->authors = explode(",", $config->get('authors'));
49
        $this->description = $config->get('description');
50
        $this->addKeywords(explode(",", $config->get('keywords')));
51
        $this->copyright = $config->get('copyright');
52
        $this->robots = $config->get('robots');
53
        $this->verify_v1 = $config->get('verify_v1');
54
    }
55
56
    /**
57
     * @param $base
58
     * @return $this
59
     */
60
    public function setTitleBase($base)
61
    {
62
        $this->titleComponents['base'] = $base;
63
        $this->generateTitle();
64
65
        return $this;
66
    }
67
68
    public function generateTitle()
69
    {
70
        $components = $this->titleComponents['elements'];
71
        if ($this->titleComponents['base']) {
72
            $components[] = $this->titleComponents['base'];
73
        }
74
        $this->title = implode($this->titleComponents['separator'], $components);
0 ignored issues
show
Documentation Bug introduced by
The property $title was declared of type boolean, but implode($this->titleComp...parator'], $components) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
75
    }
76
77
    /**
78
     * @param $keywords
79
     * @return $this
80
     */
81
    public function addKeywords($keywords)
82
    {
83
        if (!is_array($keywords)) {
84
            $keywords = [$keywords];
85
        }
86
        foreach ($keywords as $keyword) {
87
            if ($keyword) {
88
                array_unshift($this->keywords, $keyword);
89
            }
90
        }
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param $title
97
     * @return $this
98
     */
99
    public function appendTitle($title)
100
    {
101
        $this->titleComponents['elements'][] = $title;
102
        $this->generateTitle();
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param $title
109
     * @return $this
110
     */
111
    public function prependTitle($title)
112
    {
113
        array_unshift($this->titleComponents['elements'], $title);
114
        $this->generateTitle();
115
116
        return $this;
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122
    public function getFirstTitle()
123
    {
124
        $components = $this->titleComponents['elements'];
125
126
        return end($components);
127
    }
128
129
    /**
130
     * @param $description
131
     */
132
    public function addDescription($description)
133
    {
134
        array_unshift($this->descriptionComponents, $description);
135
        $this->generateDescription();
136
    }
137
138
    public function generateDescription()
139
    {
140
        $this->description = implode('. ', $this->descriptionComponents);
0 ignored issues
show
Documentation Bug introduced by
The property $description was declared of type boolean, but implode('. ', $this->descriptionComponents) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
141
    }
142
143
    /**
144
     * @param $description
145
     * @return $this
146
     */
147
    public function setDescription($description)
148
    {
149
        if ($description) {
150
            $this->description = $description;
151
        }
152
153
        return $this;
154
    }
155
156
    /**
157
     * @param array $feeds
158
     * @return $this
159
     */
160
    public function addFeeds(array $feeds)
161
    {
162
        foreach ($feeds as $feed) {
163
            $this->addFeed($feed);
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param $url
171
     * @param string $title
172
     * @return $this
173
     */
174
    public function addFeed($url, $title = 'Rss')
175
    {
176
        if (is_object($url)) {
177
            $feed = $url;
178
        } else {
179
            $feed = new stdClass();
180
            $feed->title = $title;
181
            $feed->url = $url;
182
        }
183
184
        $this->feeds[$feed->url] = $feed;
185
186
        return $this;
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    public function __toString()
193
    {
194
        if ($this->title) {
195
            $return[] = '<title>'.$this->title.'</title>';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
196
        }
197
198
        $return[] = '<meta http-equiv="Content-Type" content="text/html;" />';
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
199
        $return[] = '<meta charset="'.$this->charset.'">';
200
        $return[] = '<meta http-equiv="content-language" content="'.$this->language.'" />';
201
202
        $return[] = '<meta name="viewport" 
203
                        content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>';
204
205
        if ($this->authors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->authors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
206
            $return[] = '<meta name="author" content="'.implode(", ", $this->authors).'" />';
207
        }
208
        if ($this->publisher) {
209
            $return[] = '<meta name="publisher" content="'.$this->publisher.'" />';
210
        }
211
        if ($this->copyright) {
212
            $return[] = '<meta name="copyright" content="'.$this->copyright.'" />';
213
        }
214
215
        $return[] = '<meta name="robots" content="'.$this->robots.'" />';
216
217
        if ($this->keywords) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->keywords of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
218
            $return[] = '<meta name="keywords" content="'.implode(",", $this->keywords).'" />';
219
        }
220
221
        if (!empty($this->description)) {
222
            $return[] = '<meta name="description" content="'.$this->description.'" />';
223
        }
224
225
        if (!empty($this->verify_v1)) {
226
            $return[] = '<meta name="verify-v1" content="'.$this->verify_v1.'" />';
227
        }
228
229
        foreach ($this->feeds as $feed) {
230
            $return[] = '<link rel="alternate" 
231
                type="application/rss+xml" title="'.$feed->title.'" href="'.$feed->url.'" />';
232
        }
233
234
//        $return[] = '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />';
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
235
236
        return implode("\n", $return);
237
    }
238
}
239