PathFinder::resolveDirPathByLevel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace Pumpkin\Test;
3
4
/**
5
 * Class PathFinder
6
 * @package Pumpkin
7
 * @author Raphaël Lefebvre <[email protected]>
8
 */
9
class PathFinder extends TestHelper
10
{
11
12
    /**
13
     * @return string
14
     */
15 21
    public function getTestDirPath()
16
    {
17 21
        return dirname($this->getTest()->getReflectedTestMethod()->getFileName());
18
    }
19
20
    /**
21
     * @param string $filePatternPath
22
     * @param string[] $supportedExtensions
23
     * @throws \RuntimeException
24
     * @return string
25
     */
26 20
    public function findPath($filePatternPath, array $supportedExtensions)
27
    {
28 20
        $filePatternPath = DIRECTORY_SEPARATOR . $filePatternPath;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $filePatternPath. This often makes code more readable.
Loading history...
29 20
        $level = 0;
30 20
        while ($dirPath = realpath($this->resolveDirPathByLevel($level++))) {
31 20
            $result = $this->findFilePath($dirPath . $filePatternPath, $supportedExtensions);
32 20
            if ($result) {
33 15
                return $result;
34
            }
35 9
        }
36 5
        throw new \RuntimeException(
37 5
            sprintf('File not found for "%s"', $this->resolveDirPathByLevel(0) . $filePatternPath . '.*')
38 5
        );
39
    }
40
41
    /**
42
     * @param int $level
43
     * @return string
44
     */
45 20
    private function resolveDirPathByLevel($level)
46
    {
47 20
        return $this->getTestDirPath()
48
        . DIRECTORY_SEPARATOR
49 20
        . implode(DIRECTORY_SEPARATOR, $this->getLevelPaths($level));
50
    }
51
52
    /**
53
     * @param int $level
54
     * @return string[]
55
     */
56 20
    private function getLevelPaths($level)
57
    {
58 20
        if ($level) {
59 9
            return array_fill(0, $level, '..');
60
        }
61 20
        return array();
62
    }
63
64
    /**
65
     * @param string $filePath
66
     * @param string[] $supportedExtensions
67
     * @return string
68
     */
69 20
    private function findFilePath($filePath, array $supportedExtensions)
70
    {
71 20
        foreach ($supportedExtensions as $ext) {
72 20
            $path = realpath($filePath . '.' . ltrim($ext, '.'));
73 20
            if ($path && is_readable($path)) {
74 15
                return $path;
75
            }
76 9
        }
77 9
        return '';
78
    }
79
}
80