Completed
Push — master ( b035c8...ddcf44 )
by Paweł
02:10
created

URLType::getValue()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 0
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
ccs 13
cts 13
cp 1
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Wszetko\Sitemap\Items\DataTypes;
5
6
use Wszetko\Sitemap\Helpers\Url;
7
use Wszetko\Sitemap\Traits\Domain;
8
9
/**
10
 * Class URLType
11
 *
12
 * @package Wszetko\Sitemap\Items\DataTypes
13
 */
14
class URLType extends StringType
15
{
16
    /**
17
     * Determine if URL CAN be external
18
     *
19
     * @var bool
20
     */
21
    protected $external = false;
22
23
    /**
24
     * @return mixed|string|null
25
     */
26 16
    public function getValue()
27
    {
28 16
        if ($this->isExternal() && Url::normalizeUrl($this->value)) {
0 ignored issues
show
Bug introduced by
It seems like $this->value can also be of type array or null; however, Wszetko\Sitemap\Helpers\Url::normalizeUrl() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug Best Practice introduced by
The expression \Wszetko\Sitemap\Helpers...malizeUrl($this->value) of type false|string 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...
29 2
            $value = $this->value;
30
        } else {
31 16
            $value = str_replace($this->getDomain(), '', $this->value);
32
33 16
            if (!empty($value)) {
34 14
                $value = $this->getDomain() . '/' . ltrim($value, '/');
35
            } else {
36 7
                $value = null;
37
            }
38
        }
39
40 16
        if (!empty($value)) {
41 14
            $attributes = $this->getAttributes();
42
43 14
            if (!empty($attributes)) {
44 2
                return [$value => $attributes];
45
            } else {
46 14
                return $value;
47
            }
48
        }
49 7
    }
50
51
    /**
52
     * @return bool
53
     */
54 16
    public function isExternal(): bool
55
    {
56 16
        return $this->external;
57
    }
58
}