Nts::__construct()   C
last analyzed

Complexity

Conditions 8
Paths 17

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.8638

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 16
cts 21
cp 0.7619
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 21
nc 17
nop 1
crap 8.8638
1
<?php
2
/**
3
 * MOJEPANSTWO-API
4
 *
5
 * Copyright © 2017 pudelek.org.pl
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For the full copyright and license information, please view source file
10
 * that is bundled with this package in the file LICENSE
11
 *
12
 * @author  Marcin Pudełek <[email protected]>
13
 */
14
15
declare (strict_types=1);
16
17
namespace mrcnpdlk\MojePanstwo\Model\Address;
18
19
20
use mrcnpdlk\MojePanstwo\Api;
21
22
class Nts
23
{
24
    /**
25
     * @var string
26
     */
27
    public $region_id;
28
    /**
29
     * @var string
30
     */
31
    public $podregion_id;
32
    /**
33
     * @var string
34
     */
35
    public $teryt_wojewodztwo_id;
36
    /**
37
     * @var string
38
     */
39
    public $teryt_powiat_id;
40
    /**
41
     * @var string
42
     */
43
    public $teryt_gmina_id;
44
    /**
45
     * @var string
46
     */
47
    public $teryt_gmina_typ_id;
48
    /**
49
     * @var int
50
     */
51
    public $teryt_terc_id;
52
53
    /**
54
     * Nts constructor.
55
     *
56
     * @param string|null $nts
57
     *
58
     * @throws \mrcnpdlk\MojePanstwo\Exception
59
     */
60 5
    public function __construct(string $nts = null)
61
    {
62 5
        if (null === $nts || \strlen($nts) < 10) {
63
            Api::getInstance()
64
               ->getClient()
65
               ->getLogger()
66
               ->warning(sprintf('Invalid value of NTS code [%s]', $nts))
67
            ;
68
        } else {
69
            /** @noinspection SubStrUsedAsArrayAccessInspection */
70 5
            $this->teryt_gmina_typ_id   = substr($nts, -1, 1);
71 5
            $this->teryt_gmina_id       = substr($nts, -3, 2);
72 5
            $this->teryt_powiat_id      = substr($nts, -5, 2);
73 5
            $this->podregion_id         = substr($nts, -7, 2);
74 5
            $this->teryt_wojewodztwo_id = substr($nts, -9, 2);
75
            /** @noinspection SubStrUsedAsArrayAccessInspection */
76 5
            $this->region_id = substr($nts, -10, 1);
77 5
            if ($this->teryt_powiat_id === '00') {
78
                $this->teryt_powiat_id = null;
79
            }
80 5
            if ($this->teryt_gmina_id === '00') {
81 1
                $this->teryt_gmina_id = null;
82
            }
83 5
            if ($this->teryt_gmina_typ_id === '0') {
84 1
                $this->teryt_gmina_typ_id = null;
85
            }
86 5
            if ($this->teryt_powiat_id && $this->teryt_gmina_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->teryt_powiat_id of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null 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...
Bug Best Practice introduced by
The expression $this->teryt_gmina_id of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null 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...
87 4
                $this->teryt_terc_id = (int)($this->teryt_wojewodztwo_id . $this->teryt_powiat_id . $this->teryt_gmina_id . $this->teryt_gmina_typ_id);
88
            }
89
90
        }
91
92 5
    }
93
}
94