Completed
Push — master ( 7d5317...877556 )
by Arne
02:32
created

StandardIndexBuilder::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Storeman\IndexBuilder;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\NullLogger;
8
use Storeman\Exception;
9
use Storeman\Hash\HashContainer;
10
use Storeman\Index\Index;
11
use Storeman\Index\IndexObject;
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\Finder\SplFileInfo;
14
15
class StandardIndexBuilder implements IndexBuilderInterface, LoggerAwareInterface
16
{
17
    use LoggerAwareTrait;
18
19
    public function __construct()
20
    {
21
        $this->logger = new NullLogger();
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function buildIndex(string $path, array $excludedPathsRegexp = []): Index
28
    {
29
        $this->logger->info(sprintf("Building index using %s for path '%s' (excluded: %s)...", static::class, $path, implode(',', $excludedPathsRegexp) ?: '-'));
30
31
        if (!file_exists($path))
32
        {
33
            throw new \InvalidArgumentException("Given path '{$path}' does not exist.");
34
        }
35
        elseif (!is_dir($path))
36
        {
37
            throw new \InvalidArgumentException("Given path '{$path}' is not a directory.");
38
        }
39
        elseif (!is_readable($path))
40
        {
41
            throw new \InvalidArgumentException("Given directory '{$path}' is not readable.'");
42
        }
43
44
        $finder = new Finder();
45
        $finder->in($path);
46
        $finder->ignoreDotFiles(false);
47
48
        foreach ($excludedPathsRegexp as $excludedPathRegexp)
49
        {
50
            $finder->notPath($excludedPathRegexp);
51
        }
52
53
        $index = new Index();
54
55
        foreach ($finder->directories() as $fileInfo)
56
        {
57
            /** @var SplFileInfo $fileInfo */
58
59
            if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname()))
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $indexObject is correct as $this->buildIndexObject(...>getRelativePathname()) (which targets Storeman\IndexBuilder\St...der::buildIndexObject()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
            {
61
                $index->addObject($indexObject);
62
            }
63
        }
64
65
        foreach ($finder->files() as $fileInfo)
66
        {
67
            /** @var SplFileInfo $fileInfo */
68
69
            if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname()))
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $indexObject is correct as $this->buildIndexObject(...>getRelativePathname()) (which targets Storeman\IndexBuilder\St...der::buildIndexObject()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
            {
71
                $index->addObject($indexObject);
72
            }
73
        }
74
75
        return $index;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function buildIndexObject(string $basePath, string $relativePath): ?IndexObject
82
    {
83
        $absolutePath = rtrim($basePath, '/') . '/' . $relativePath;
84
85
        clearstatcache(null, $absolutePath);
86
87
        if (!($stat = @lstat($absolutePath)))
88
        {
89
            throw new Exception("lstat() failed for {$absolutePath}");
90
        }
91
92
        $size = $linkTarget = $hashContainer = null;
93
94
        switch ($stat['mode'] & 0xF000)
95
        {
96
            case 0x4000:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
97
98
                $type = IndexObject::TYPE_DIR;
99
100
                break;
101
102
            case 0x8000:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
103
104
                $type = IndexObject::TYPE_FILE;
105
                $size = $stat['size'];
106
                $hashContainer = new HashContainer();
107
108
                break;
109
110
            case 0xA000:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
111
112
                $type = IndexObject::TYPE_LINK;
113
                $linkTarget = readlink($absolutePath);
114
115
                if ($linkTarget === false)
116
                {
117
                    $this->logger->notice("Found broken link: {$absolutePath}");
118
119
                    // silently ignore broken links
120
                    return null;
121
                }
122
123
                break;
124
125
            default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
126
127
                // sockets, pipes, etc.
128
                return null;
129
        }
130
131
        return new IndexObject($relativePath, $type, $stat['mtime'], $stat['ctime'], $stat['mode'] & 0777, $size, $stat['ino'], $linkTarget, null, $hashContainer);
132
    }
133
}
134