Completed
Push — master ( e610ee...c79300 )
by Arne
04:09
created

StandardIndexBuilder::buildIndexObject()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 52
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 8.6868
c 0
b 0
f 0
cc 6
eloc 24
nc 6
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Storeman\IndexBuilder;
4
5
use Storeman\Exception;
6
use Storeman\Hash\HashContainer;
7
use Storeman\Index\Index;
8
use Storeman\Index\IndexObject;
9
use Symfony\Component\Finder\Finder;
10
use Symfony\Component\Finder\SplFileInfo;
11
12
class StandardIndexBuilder implements IndexBuilderInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function buildIndex(string $path, array $excludedPathsRegexp = []): Index
18
    {
19
        if (!file_exists($path))
20
        {
21
            throw new \InvalidArgumentException("Given path '{$path}' does not exist.");
22
        }
23
        elseif (!is_dir($path))
24
        {
25
            throw new \InvalidArgumentException("Given path '{$path}' is not a directory.");
26
        }
27
        elseif (!is_readable($path))
28
        {
29
            throw new \InvalidArgumentException("Given directory '{$path}' is not readable.'");
30
        }
31
32
        $finder = new Finder();
33
        $finder->in($path);
34
        $finder->ignoreDotFiles(false);
35
36
        foreach ($excludedPathsRegexp as $excludedPathRegexp)
37
        {
38
            $finder->notPath($excludedPathRegexp);
39
        }
40
41
        $index = new Index();
42
43
        foreach ($finder->directories() as $fileInfo)
44
        {
45
            /** @var SplFileInfo $fileInfo */
46
47
            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...
48
            {
49
                $index->addObject($indexObject);
50
            }
51
        }
52
53
        foreach ($finder->files() as $fileInfo)
54
        {
55
            /** @var SplFileInfo $fileInfo */
56
57
            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...
58
            {
59
                $index->addObject($indexObject);
60
            }
61
        }
62
63
        return $index;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function buildIndexObject(string $basePath, string $relativePath): ?IndexObject
70
    {
71
        $absolutePath = rtrim($basePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relativePath;
72
73
        clearstatcache(null, $absolutePath);
74
75
        if (!($stat = @lstat($absolutePath)))
76
        {
77
            throw new Exception("lstat() failed for {$absolutePath}");
78
        }
79
80
        $size = $linkTarget = $hashContainer = null;
81
82
        switch ($stat['mode'] & 0xF000)
83
        {
84
            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...
85
86
                $type = IndexObject::TYPE_DIR;
87
88
                break;
89
90
            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...
91
92
                $type = IndexObject::TYPE_FILE;
93
                $size = $stat['size'];
94
                $hashContainer = new HashContainer();
95
96
                break;
97
98
            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...
99
100
                $type = IndexObject::TYPE_LINK;
101
                $linkTarget = readlink($absolutePath);
102
103
                if ($linkTarget === false)
104
                {
105
                    // todo: log
106
107
                    // silently ignore broken links
108
                    return null;
109
                }
110
111
                break;
112
113
            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...
114
115
                // sockets, pipes, etc.
116
                return null;
117
        }
118
119
        return new IndexObject($relativePath, $type, $stat['mtime'], $stat['ctime'], $stat['mode'], $size, $stat['ino'], $linkTarget, null, $hashContainer);
120
    }
121
}
122