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

Assets   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 32
ccs 0
cts 26
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 10 1
A map() 0 13 3
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