LessFile   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 5
dl 0
loc 22
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A load() 0 14 2
1
<?php
2
/**
3
 * JBZoo Assets
4
 *
5
 * This file is part of the JBZoo CCK package.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package    Assets
10
 * @license    MIT
11
 * @copyright  Copyright (C) JBZoo.com, All rights reserved.
12
 * @link       https://github.com/JBZoo/Assets
13
 */
14
15
namespace JBZoo\Assets\Asset;
16
17
use JBZoo\Less\Less;
18
19
/**
20
 * Class LessFile
21
 * @package JBZoo\Assets\Asset
22
 */
23
class LessFile extends File
24
{
25
    protected $type = Asset::TYPE_LESS_FILE;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function load(array $filters = [])
31
    {
32
        $result = parent::load($filters);
33
        $compiled = null;
34
35
        if ($result[1]) {
36
            $options = $this->eManager->getParams();
37
            $root = $this->eManager->getPath()->getRoot();
38
            $less = new Less($options->get('less'));
39
            $compiled = $less->compile($result[1], $root);
0 ignored issues
show
Bug introduced by SmetDenis
It seems like $result[1] can also be of type array; however, JBZoo\Less\Less::compile() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40
        }
41
42
        return [Asset::TYPE_CSS_FILE, $compiled];
0 ignored issues
show
Best Practice introduced by SmetDenis
The expression return array(\JBZoo\Asse...E_CSS_FILE, $compiled); seems to be an array, but some of its elements' types (null) are incompatible with the return type of the parent method JBZoo\Assets\Asset\File::load of type array<string|array>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
43
    }
44
}
45