HtmlFileFinder   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFiles() 0 23 5
A isHtmlFile() 0 9 2
1
<?php
2
3
namespace Suitmedia\LighthouseAudit;
4
5
use LogicException;
6
use Suitmedia\LighthouseAudit\Concerns\CanResolveDocumentRoot;
7
8
class HtmlFileFinder
9
{
10
    use CanResolveDocumentRoot;
11
12
    /**
13
     * Path to find HTML files.
14
     *
15
     * @var string
16
     */
17
    protected $path;
18
19
    /**
20
     * HtmlFileFinder constructor.
21
     *
22
     * @param mixed $path
23
     *
24
     * @throws LogicException
25
     */
26
    public function __construct($path)
27
    {
28
        $this->path = $this->getDocumentRoot($path);
29
    }
30
31
    /**
32
     * Get the HTML files.
33
     *
34
     * @return array
35
     */
36
    public function getFiles() :array
37
    {
38
        $dir = opendir($this->path);
39
        $files = [];
40
41
        if ($dir === false) {
42
            return $files;
43
        }
44
45
        while (($file = readdir($dir)) !== false) {
46
            if (is_dir($this->path.$file) || !$this->isHtmlFile($file)) {
47
                continue;
48
            }
49
50
            $files[] = $file;
51
        }
52
53
        closedir($dir);
54
55
        sort($files);
56
57
        return $files;
58
    }
59
60
    /**
61
     * Check if the given file is an HTML file.
62
     *
63
     * @param mixed $path
64
     *
65
     * @return bool
66
     */
67
    protected function isHtmlFile($path) :bool
68
    {
69
        if (!is_string($path)) {
70
            return false;
71
        }
72
        $extension = pathinfo($path, PATHINFO_EXTENSION);
73
74
        return in_array($extension, ['php', 'html']);
75
    }
76
}
77