Passed
Push — master ( ed297f...604ea4 )
by Giuliano
04:39
created

DirectiveFinder::scan()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 21
rs 9.9332

1 Method

Rating   Name   Duplication   Size   Complexity  
A DirectiveFinder::__construct() 0 3 1
1
<?php
2
3
namespace Maestriam\Samurai\Foundation;
4
5
use Maestriam\Samurai\Entities\Theme;
6
use Maestriam\FileSystem\Support\FileSystem;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Maestriam\Samurai\Foundation\FileSystem. 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...
7
use Maestriam\Samurai\Entities\Component;
8
use Maestriam\Samurai\Entities\Includer;
9
10
class DirectiveFinder
11
{
12
    private string $pattern = '.blade';
13
14
    private DirectiveParser $parserInstance;
15
16
    public function __construct(Theme $theme)
17
    {
18
        $this->init($theme);
19
    }
20
21
    /**
22
     * Recupera TODAS as diretivas de um tema.  
23
     *
24
     * @return array
25
     */
26
    public function all() : array
27
    {          
28
        $directives = [];
29
30
        $files = $this->readFiles();
31
32
        foreach($files as $file) {
33
            $directives[] = $this->parser()->parse($file)->toDirective();
34
        }
35
36
        return $directives;
37
    }
38
39
    /**
40
     * Retorna a instância de uma diretiva includer, de acordo com o tema e a sentença.  
41
     *
42
     * @param  string $sentence
43
     * @return Includer
44
     */
45
    public function include(string $sentence) : Includer
46
    {
47
        return new Includer($this->theme(), $sentence);
48
    }
49
50
    /**
51
     * Retorna a instância de uma diretiva component, de acordo com o tema e a sentença.  
52
     *
53
     * @param  string $sentence
54
     * @return Component
55
     */
56
    public function component(string $sentence) : Component
57
    {
58
        return new Component($this->theme(), $sentence);
59
    }
60
61
    /**
62
     * Retorna a instância do tema que será manipulado.  
63
     *
64
     * @return Theme
65
     */
66
    private function theme() : Theme
67
    {
68
        return $this->themeInstance;
69
    }
70
71
    /**
72
     * Inicia os atributos necessários para instanciar a classe.  
73
     *
74
     * @param  Theme $theme
75
     * @return DirectiveFinder
76
     */
77
    private function init(Theme $theme) : DirectiveFinder
78
    {
79
        $this->themeInstance = $theme;
0 ignored issues
show
Bug Best Practice introduced by
The property themeInstance does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
        $this->parserInstance = new DirectiveParser($theme);
81
     
82
        return $this;
83
    }    
84
85
    /**
86
     * Retorna a instância para identificação de uma diretiva,
87
     * através do caminho absoluto do arquivo da diretiva.  
88
     *
89
     * @return DirectiveParser
90
     */
91
    private function parser() : DirectiveParser
92
    {
93
        return $this->parserInstance;
94
    }
95
96
    /**
97
     * Retorna a lista, com o caminho completo, de todas as diretivas
98
     * inseridas no tema determinado.  
99
     *
100
     * @return array
101
     */
102
    private function readFiles() : array
103
    {        
104
        $path = $this->theme()->paths()->source();
105
106
        return FileSystem::folder($path)->files($this->pattern);
107
    }
108
}
109