Passed
Pull Request — master (#8)
by vincent
02:31
created

Collection::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace vfalies\tmdb\Items;
4
5
use vfalies\tmdb\Interfaces\CollectionInterface;
6
use vfalies\tmdb\Tmdb;
7
use vfalies\tmdb\Results\Movie;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, vfalies\tmdb\Items\Movie.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
9
class Collection extends Item implements CollectionInterface
10
{
11
12
    // Private loaded data
13
    protected $data = null;
14
    protected $conf = null;
15
    protected $id   = null;
16
    protected $tmdb = null;
17
18
    /**
19
     * Constructor
20
     * @param \vfalies\tmdb\Tmdb $tmdb
21
     * @throws \Exception
22
     */
23 15
    public function __construct(Tmdb $tmdb, $collection_id, array $options = array())
24
    {
25 15
        parent::__construct($tmdb, $collection_id, $options, 'collection');
26 14
    }
27
28
    /**
29
     * Get collection ID
30
     * @return int
31
     * @throws \Exception
32
     */
33 2
    public function getId(): int
34
    {
35 2
        return $this->id;
36
    }
37
38
    /**
39
     * Get collection name
40
     * @return string
41
     * @throws \Exception
42
     */
43 2
    public function getName(): string
44
    {
45 2
        if (isset($this->data->name))
46
        {
47 1
            return $this->data->name;
48
        }
49 1
        throw new \Exception('Collection name can not be found');
50
    }
51
52
    /**
53
     * Get collection parts
54
     * @return Generator|SearchMovieResult
55
     */
56 2
    public function getParts(): \Generator
57
    {
58 2
        if (!empty($this->data->parts))
59
        {
60 1
            foreach ($this->data->parts as $part)
61
            {
62 1
                $movie = new Movie($this->tmdb, $part);
63 1
                yield $movie;
64
            }
65
        }
66 1
    }
67
68
}
69