Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

src/Task/Assets/Less.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Task\Assets;
3
4
use Robo\Result;
5
6
/**
7
 * Compiles less files.
8
 *
9
 * ```php
10
 * <?php
11
 * $this->taskLess([
12
 *     'less/default.less' => 'css/default.css'
13
 * ])
14
 * ->run();
15
 * ?>
16
 * ```
17
 *
18
 * Use one of both less compilers in your project:
19
 *
20
 * ```
21
 * "leafo/lessphp": "~0.5",
22
 * "oyejorge/less.php": "~1.5"
23
 * ```
24
 *
25
 * Specify directory (string or array) for less imports lookup:
26
 *
27
 * ```php
28
 * <?php
29
 * $this->taskLess([
30
 *     'less/default.less' => 'css/default.css'
31
 * ])
32
 * ->importDir('less')
33
 * ->compiler('lessphp')
34
 * ->run();
35
 * ?>
36
 * ```
37
 *
38
 * You can implement additional compilers by extending this task and adding a
39
 * method named after them and overloading the lessCompilers() method to
40
 * inject the name there.
41
 */
42
class Less extends CssPreprocessor
43
{
44
    const FORMAT_NAME = 'less';
45
46
    /**
47
     * @var string[]
48
     */
49
    protected $compilers = [
50
        'less', // https://github.com/oyejorge/less.php
51
        'lessphp', //https://github.com/leafo/lessphp
52
    ];
53
54
    /**
55
     * lessphp compiler
56
     * @link https://github.com/leafo/lessphp
57
     *
58
     * @param string $file
59
     *
60
     * @return string
61
     */
62
    protected function lessphp($file)
63
    {
64
        if (!class_exists('\lessc')) {
65
            return Result::errorMissingPackage($this, 'lessc', 'leafo/lessphp');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Robo\Result::err...ssc', 'leafo/lessphp'); (Robo\Result) is incompatible with the return type documented by Robo\Task\Assets\Less::lessphp of type string.

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 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('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...
66
        }
67
68
        $lessCode = file_get_contents($file);
69
70
        $less = new \lessc();
71
        if (isset($this->compilerOptions['importDirs'])) {
72
            $less->setImportDir($this->compilerOptions['importDirs']);
73
        }
74
75
        return $less->compile($lessCode);
76
    }
77
78
    /**
79
     * less compiler
80
     * @link https://github.com/oyejorge/less.php
81
     *
82
     * @param string $file
83
     *
84
     * @return string
85
     */
86
    protected function less($file)
87
    {
88
        if (!class_exists('\Less_Parser')) {
89
            return Result::errorMissingPackage($this, 'Less_Parser', 'oyejorge/less.php');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Robo\Result::err..., 'oyejorge/less.php'); (Robo\Result) is incompatible with the return type documented by Robo\Task\Assets\Less::less of type string.

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 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('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...
90
        }
91
92
        $lessCode = file_get_contents($file);
93
94
        $parser = new \Less_Parser();
95
        $parser->SetOptions($this->compilerOptions);
96
        if (isset($this->compilerOptions['importDirs'])) {
97
            $importDirs = [];
98
            foreach ($this->compilerOptions['importDirs'] as $dir) {
99
                $importDirs[$dir] = $dir;
100
            }
101
            $parser->SetImportDirs($importDirs);
102
        }
103
104
        $parser->parse($lessCode);
105
106
        return $parser->getCss();
107
    }
108
}
109