Completed
Push — master ( 6e390f...5a6d30 )
by Dmitry
07:10 queued 04:24
created

Assets   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

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
7
class Assets
8
{
9
    private $fs;
10 2
    public function __construct(Filesystem $fs)
11
    {
12 2
        $this->fs = $fs;
13 2
    }
14 2
    public function run()
15
    {
16
        $artifacts = [
17 2
            'js' => $this->map('js'),
18 2
            'styl' => $this->map('styl'),
19
        ];
20
21 2
        $artifacts['hash'] = md5(json_encode($artifacts));
22 2
        return $artifacts;
23
    }
24
25 2
    public function map($type)
26
    {
27 2
        $mapping = [];
28 2
        if (is_dir($type)) {
29 2
            exec('find '.$type.' -name "*.'.$type.'" -exec md5sum {} \; | sort', $contents);
30 2
            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...
31 2
                list($hash, $file) = explode("  ", $row);
32 2
                $file = substr($file, strlen($type) + 1);
33 2
                $mapping[$file] = $hash;
34
            }
35
        }
36 2
        return $mapping;
37
    }
38
}
39