Completed
Pull Request — master (#21)
by
unknown
04:45
created

ComposerConfigFileFinder::find()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.1928
c 0
b 0
f 0
cc 5
nc 7
nop 1
1
<?php
2
3
namespace Arrilot\BitrixMigrations;
4
5
use RuntimeException;
6
use SplFileInfo;
7
8
class ComposerConfigFileFinder
9
{
10
    const COMPOSER_JSON_FILENAME = 'composer.json';
11
12
    /**
13
     * @param string $sourceDir
14
     * @return SplFileInfo|null
15
     * @throws RuntimeException
16
     */
17
    public function find($sourceDir)
18
    {
19
        $sourceDir = realpath($sourceDir);
20
21
        if ($sourceDir === false) {
22
            throw new RuntimeException('Не удалось получить канонизированный абсолютный путь к директории поиска');
23
        }
24
25
        while (!file_exists($sourceDir . DIRECTORY_SEPARATOR . self::COMPOSER_JSON_FILENAME)) {
26
            $parentDir = dirname($sourceDir);
27
28
            if ($parentDir === $sourceDir) {
29
                break;
30
            }
31
32
            $sourceDir = $parentDir;
33
        }
34
35
        $filePath = $sourceDir . DIRECTORY_SEPARATOR . self::COMPOSER_JSON_FILENAME;
36
37
        if (file_exists($filePath)) {
38
            return new SplFileInfo($filePath);
39
        }
40
41
        return null;
42
    }
43
}