Passed
Push — master ( 4bccec...259a7c )
by David
01:44
created

Theme   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 6.17 %

Importance

Changes 0
Metric Value
dl 5
loc 81
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B fromDirectory() 5 24 5
A __construct() 0 4 1
A getName() 0 3 1
A getPath() 0 3 1
A getZones() 0 3 1
A existsInDirectory() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace TheCodingMachine\CMS\StaticRegistry\Loaders;
3
4
use Symfony\Component\Yaml\Yaml;
5
6
class Theme
7
{
8
    /**
9
     * @var string
10
     */
11
    private $path;
12
    /**
13
     * @var string[]
14
     */
15
    private $zones;
16
17
    /**
18
     * @param string[] $zones
19
     */
20
    public function __construct(string $path, array $zones)
21
    {
22
        $this->path = $path;
23
        $this->zones = $zones;
24
    }
25
26
    public static function fromDirectory(string $path): self
27
    {
28
        if (!is_dir($path)) {
29
            throw new UnableToLoadFileException('Cannot find directory '.$path);
30
        }
31
        $path = rtrim($path, '/\\');
32
        $configPath = $path.'/config.yml';
33
        if (!is_file($configPath)) {
34
            throw new UnableToLoadFileException('Cannot find config.yml in theme '.$path);
35
        }
36
37
        $yaml = Yaml::parse(file_get_contents($configPath));
38
39
        $compulsoryFields = ['zones'];
40
41 View Code Duplication
        foreach ($compulsoryFields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            if (!isset($yaml[$field])) {
43
                throw new UnableToLoadFileException('Missing field '.$field.' in YAML file '.$configPath);
44
            }
45
        }
46
47
        return new self(
48
            $path,
49
            $yaml['zones']
50
        );
51
    }
52
53
    /**
54
     * Returns true if $path is a theme directory.
55
     *
56
     * @param string $path The path, with no trailing /
57
     * @return bool
58
     */
59
    public static function existsInDirectory(string $path): bool
60
    {
61
        $configPath = $path.'/config.yml';
62
        return file_exists($configPath);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getName(): string
69
    {
70
        return basename($this->path);
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getPath(): string
77
    {
78
        return $this->path;
79
    }
80
81
    /**
82
     * @return string[]
83
     */
84
    public function getZones(): array
85
    {
86
        return $this->zones;
87
    }
88
}
89