Completed
Push — feature/5.0.1 ( 559a15...7b9e0a )
by Raúl
01:17
created

Header::asArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sepia\PoParser\Catalog;
4
5
class Header
6
{
7
    /** @var array */
8
    protected $headers;
9
10
    /** @var int|null */
11
    protected $nPlurals = null;
12
13
    public function __construct(array $headers = array())
14
    {
15
        $this->headers = $headers;
16
    }
17
18
    public function getPluralFormsCount()
19
    {
20
        if ($this->nPlurals !== null) {
21
            return $this->nPlurals;
22
        }
23
24
        $header = $this->getHeaderValue('Plural-Forms');
25
        if ($header === null) {
26
            $this->nPlurals = 0;
27
            return $this->nPlurals;
28
        }
29
30
        $matches = array();
31
        if (preg_match('/nplurals=([0-9]+)/', $header, $matches) !== 1) {
32
            $this->nPlurals = 0;
33
            return $this->nPlurals;
34
        }
35
36
        $this->nPlurals = isset($matches[1]) ? $matches[1] : 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like isset($matches[1]) ? $matches[1] : 0 can also be of type string. However, the property $nPlurals is declared as type integer|null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
37
38
        return $this->nPlurals;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function asArray()
45
    {
46
        return $this->headers;
47
    }
48
49
    /**
50
     * @param string $headerName
51
     *
52
     * @return string|null
53
     */
54
    protected function getHeaderValue($headerName)
55
    {
56
        $header = array_values(array_filter(
57
            $this->headers,
58
            function ($string) use ($headerName) {
59
                return preg_match('/'.$headerName.':(.*)/i', $string) == 1;
60
            }
61
        ));
62
63
        return count($header) ? $header[0] : null;
64
    }
65
}
66