SubTheme::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TheCodingMachine\CMS\StaticRegistry\Loaders;
3
4
use Barclays\Model\BaseBlock;
0 ignored issues
show
Bug introduced by
The type Barclays\Model\BaseBlock was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use Mni\FrontYAML\Parser;
6
use Symfony\Component\Yaml\Yaml;
7
use TheCodingMachine\CMS\Block\Block;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TheCodingMachine\CMS\StaticRegistry\Loaders\Block. Consider defining an alias.

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 SubTheme
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name;
15
    /**
16
     * @var string
17
     */
18
    private $parent;
19
    /**
20
     * @var array|string[][]
21
     */
22
    private $assignations;
23
24
    /**
25
     * @param string $name
26
     * @param string $parent
27
     * @param string[][] $assignations
28
     */
29
    public function __construct(string $name, string $parent, array $assignations)
30
    {
31
        $this->name = $name;
32
        $this->parent = $parent;
33
        $this->assignations = $assignations;
34
    }
35
36
    public static function fromFile(string $file): self
37
    {
38
        if (!is_readable($file)) {
39
            throw new UnableToLoadFileException('Cannot read file '.$file);
40
        }
41
42
        $yaml = Yaml::parse(file_get_contents($file));
43
44
        $compulsoryFields = ['name', 'parent', 'assignations'];
45
46
        foreach ($compulsoryFields as $field) {
47
            if (!isset($yaml[$field])) {
48
                throw new UnableToLoadFileException('Missing field "'.$field.'" in YAML file '.$file);
49
            }
50
        }
51
52
        return new self(
53
            $yaml['name'],
54
            $yaml['parent'],
55
            $yaml['assignations']
56
        );
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getName(): string
63
    {
64
        return $this->name;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getParent(): string
71
    {
72
        return $this->parent;
73
    }
74
75
    /**
76
     * @return array|string[][]
77
     */
78
    public function getAssignations()
79
    {
80
        return $this->assignations;
81
    }
82
}
83