Directory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 60
rs 10
ccs 26
cts 26
cp 1
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toTexts() 0 4 2
B getContents() 0 27 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpSpellcheck\Source;
6
7
use PhpSpellcheck\Exception\RuntimeException;
8
use PhpSpellcheck\Text;
9
10
class Directory implements SourceInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $dirPath;
16
17
    /**
18
     * @var string|null
19
     */
20
    private $pattern;
21 2
22
    public function __construct(string $dirPath, ?string $pattern = null)
23 2
    {
24 2
        $this->dirPath = $dirPath;
25 2
        $this->pattern = $pattern;
26
    }
27
28
    /**
29
     * @param array<mixed> $context
30 2
     *
31
     * @return iterable<Text>
32 2
     */
33 2
    public function toTexts(array $context): iterable
34 2
    {
35 2
        foreach ($this->getContents() as $text) {
36
            yield new Text($text->getContent(), array_merge($text->getContext(), $context));
37
        }
38 2
    }
39 1
40
    /**
41
     * @return iterable<Text>
42
     */
43 2
    private function getContents(): iterable
44 2
    {
45 1
        $filesInDir = new \RecursiveIteratorIterator(
46 1
            new \RecursiveDirectoryIterator(
47
                $this->dirPath,
48 1
                \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME
49
            ),
50
            \RecursiveIteratorIterator::SELF_FIRST
51 2
        );
52 2
53
        if ($this->pattern !== null) {
54
            $filesInDir = new \RegexIterator($filesInDir, $this->pattern, \RegexIterator::GET_MATCH);
55 2
        }
56
57 2
        /** @var array<string>|\SplFileInfo|string $file */
58
        foreach ($filesInDir as $file) {
59 2
            if (\is_string($file)) {
60 2
                $file = new \SplFileInfo($file);
61 2
            } elseif (\is_array($file) && !empty($file)) {
62 2
                // When regex pattern is used, an array containing the file path in its first element is returned
63 2
                $file = new \SplFileInfo(current($file));
64
            } else {
65
                throw new RuntimeException(\Safe\sprintf('Couldn\'t create "%s" object from the given file', \SplFileInfo::class));
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

65
                throw new RuntimeException(/** @scrutinizer ignore-deprecated */ \Safe\sprintf('Couldn\'t create "%s" object from the given file', \SplFileInfo::class));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
66 2
            }
67
68
            if (!$file->isDir() && $file->getRealPath() !== false) {
69
                yield from (new File($file->getRealPath()))->toTexts();
70
            }
71
        }
72
    }
73
}
74