Completed
Push — master ( e7b929...ba2691 )
by Dmitry
03:11
created

Assets::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Basis\Job\Module;
4
5
use Basis\Filesystem;
6
use Basis\Job;
7
8
class Assets extends Job
9
{
10
    private $fs;
11
    public function __construct(Filesystem $fs)
12
    {
13
        $this->fs = $fs;
14
    }
15
    public function run()
16
    {
17
        $artifacts = [
18
            'js' => $this->map('js'),
19
            'styl' => $this->map('styl'),
20
        ];
21
22
        $artifacts['hash'] = md5(json_encode($artifacts));
23
        return $artifacts;
24
    }
25
26
    public function map($type)
27
    {
28
        $mapping = [];
29
        if (is_dir($type)) {
30
            exec('find '.$type.' -name "*.'.$type.'" -exec md5sum {} \; | sort', $contents);
31
            foreach ($contents as $i => $row) {
0 ignored issues
show
Bug introduced by
The expression $contents of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
32
                list($hash, $file) = explode("  ", $row);
33
                $file = substr($file, strlen($type) + 1);
34
                $mapping[$file] = $hash;
35
            }
36
        }
37
        return $mapping;
38
    }
39
}
40