Completed
Push — master ( 472029...d7e60d )
by Andy
01:48
created

Legislature::directory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace EveryPolitician\EveryPolitician;
4
5
class Legislature
6
{
7
    public $name;
8
    public $slug;
9
    public $personCount;
10
    public $sha;
11
    public $statementCount;
12
    public $popoloUrl;
13
    protected $legislatureData;
14
    protected $country;
15
16 18
    public function __construct($legislatureData, $country)
17
    {
18
        $propertyMappings = [
19 18
            'name' => 'name',
20 12
            'slug' => 'slug',
21 12
            'person_count' => 'personCount',
22 12
            'sha' => 'sha',
23 12
            'statement_count' => 'statementCount',
24
            'popolo_url' => 'popoloUrl'
25 12
        ];
26 18
        foreach ($propertyMappings as $k => $v) {
27 18
            $this->$v = array_key_exists($k, $legislatureData) ? $legislatureData[$k] : null;
28 12
        }
29 18
        $timestamp = $legislatureData['lastmod'];
30 18
        $this->lastmod = \DateTime::createFromFormat('U', $timestamp);
0 ignored issues
show
Bug introduced by
The property lastmod does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31 18
        $this->legislatureData = $legislatureData;
32 18
        $this->country = $country;
33 18
    }
34
35 3
    public function __toString()
36
    {
37 3
        return '<Legislature: '.$this->name.' in '.$this->country->name.'>';
38
    }
39
40
    /**
41
     * Return the directory path in the everypolitician-data repository
42
     */
43 3
    public function directory()
44
    {
45 3
        $splitPath = explode('/', $this->legislatureData['sources_directory']);
46 3
        $splitPath = array_slice($splitPath, 1, 2);
47 3
        return implode('/', $splitPath);
48
    }
49
}
50