Passed
Push — master ( 35235e...b730fe )
by Malte
03:40
created

Task   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
dl 0
loc 32
rs 10
c 2
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B consolidate() 0 26 3
1
<?php
2
3
namespace AppBundle\ConsolidateUsedFiles;
4
5
use Helper\FileSystem;
6
use Helper\NullStyle;
7
use Symfony\Component\Console\Style\StyleInterface;
8
9
/**
10
 * Consolidate the list of used files by removing duplicates and sorting them. Improves performance for later tasks.
11
 */
12
final class Task
13
{
14
    /**
15
     * @param string $userProvidedPathToConsolidate
16
     * @param StyleInterface|null $ioStyle
17
     */
18
    public function consolidate($userProvidedPathToConsolidate, StyleInterface $ioStyle = null)
19
    {
20
        if ($ioStyle === null) {
21
            $ioStyle = new NullStyle();
22
        }
23
        $ioStyle->progressStart(4);
24
25
        $pathToConsolidate = FileSystem::getRealPathToReadableAndWritableFile($userProvidedPathToConsolidate);
26
        if (!$pathToConsolidate) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pathToConsolidate of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
27
            $message = $userProvidedPathToConsolidate . ' has to be a file both readable and writable.';
28
            $ioStyle->error($message);
29
            throw new \InvalidArgumentException($message);
30
        }
31
        $ioStyle->progressAdvance();
32
33
        $usedFiles = FileSystem::readFileIntoArray($pathToConsolidate);
34
        $ioStyle->progressAdvance();
35
36
        $usedFiles = array_unique($usedFiles);
0 ignored issues
show
Bug introduced by
It seems like $usedFiles can also be of type boolean; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

36
        $usedFiles = array_unique(/** @scrutinizer ignore-type */ $usedFiles);
Loading history...
37
        sort($usedFiles);
38
        $ioStyle->progressAdvance();
39
40
        FileSystem::writeArrayToFile($usedFiles, $pathToConsolidate);
41
        $ioStyle->progressFinish();
42
43
        $ioStyle->success('Finished consolidating ' . $pathToConsolidate);
44
    }
45
}
46