Passed
Push — master ( 4a5f5f...d4ccbb )
by Sebastian
03:14
created

Localization_Source_Folder::isExcluded()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 4
c 2
b 1
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppLocalize;
6
7
use AppUtils\FileHelper;
8
use DirectoryIterator;
9
10
class Localization_Source_Folder extends Localization_Source
11
{
12
   /**
13
    * The folder under which all translatable files are kept.
14
    * @var string
15
    */
16
    protected $sourcesFolder;
17
    
18
   /**
19
    * @var string
20
    */
21
    protected $id;
22
23
   /**
24
    * @param string $alias An alias for this source, to recognize it by.
25
    * @param string $label The human-readable label, used in the editor.
26
    * @param string $group A human-readable group label to group several sources by. Used in the editor.
27
    * @param string $storageFolder The folder in which to store the localization files.
28
    * @param string $sourcesFolder The folder in which to analyze files to find translatable strings.
29
    */
30
    public function __construct(string $alias, string $label, string $group, string $storageFolder, string $sourcesFolder)
31
    {
32
        parent::__construct($alias, $label, $group, $storageFolder);
33
        
34
        $this->sourcesFolder = $sourcesFolder;
35
        $this->id = md5($sourcesFolder);
36
    }
37
    
38
    public function getID() : string
39
    {
40
        return $this->id;
41
    }
42
    
43
    public function getSourcesFolder() : string
44
    {
45
        return $this->sourcesFolder;
46
    }
47
48
    /**
49
     * @var array<string,string[]>
50
     */
51
    protected $excludes = array(
52
        'folders' => array(),
53
        'files' => array()
54
    );
55
    
56
    public function excludeFolder(string $folder) : Localization_Source_Folder
57
    {
58
        if(!in_array($folder, $this->excludes['folders'])) {
59
            $this->excludes['folders'][] = $folder;
60
        }
61
        
62
        return $this;
63
    }
64
    
65
    public function excludeFolders(array $folders) : Localization_Source_Folder
66
    {
67
        foreach($folders as $folder) {
68
            $this->excludeFolder($folder);
69
        }
70
        
71
        return $this;
72
    }
73
    
74
    public function excludeFiles(array $files) : Localization_Source_Folder
75
    {
76
        $this->excludes['files'] = array_merge($this->excludes['files'], $files);
77
        return $this;
78
    }
79
    
80
    protected function _scan() : void
81
    {
82
        $this->processFolder($this->getSourcesFolder());
83
    }
84
85
    /**
86
     * Processes the target folder, and recurses into sub folders.
87
     * @param string $folder
88
     * @throws Localization_Exception
89
     */
90
    protected function processFolder(string $folder) : void
91
    {
92
        $d = new DirectoryIterator($folder);
93
        foreach ($d as $item) 
94
        {
95
            if ($item->isDot()) {
96
                continue;
97
            }
98
            
99
            $filename = $item->getFilename();
100
            
101
            if ($item->isDir() && !in_array($filename, $this->excludes['folders'])) {
102
                $this->processFolder($item->getPathname());
103
                continue;
104
            }
105
            
106
            if ($item->isFile() && $this->parser->isFileSupported($filename) && !$this->isExcluded($filename)) {
0 ignored issues
show
Bug introduced by
The method isFileSupported() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            if ($item->isFile() && $this->parser->/** @scrutinizer ignore-call */ isFileSupported($filename) && !$this->isExcluded($filename)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
                $this->parseFile($item->getPathname());
108
            }
109
        }
110
    }
111
    
112
    protected function isExcluded(string $filename) : bool
113
    {
114
        foreach ($this->excludes['files'] as $search) {
115
            if (stristr($filename, $search)) {
116
                return true;
117
            }
118
        }
119
        
120
        return false;
121
    }
122
}
123