Completed
Push — master ( d3a073...5737c8 )
by Greg
02:21
created

src/Task/Assets/Scss.php (1 issue)

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 scss files.
8
 *
9
 * ```php
10
 * <?php
11
 * $this->taskScss([
12
 *     'scss/default.scss' => 'css/default.css'
13
 * ])
14
 * ->importDir('assets/styles')
15
 * ->run();
16
 * ?>
17
 * ```
18
 *
19
 * Use the following scss compiler in your project:
20
 *
21
 * ```
22
 * "leafo/scssphp": "~0.1",
23
 * ```
24
 *
25
 * You can implement additional compilers by extending this task and adding a
26
 * method named after them and overloading the scssCompilers() method to
27
 * inject the name there.
28
 */
29
class Scss extends CssPreprocessor
30
{
31
    const FORMAT_NAME = 'scss';
32
33
    /**
34
     * @var string[]
35
     */
36
    protected $compilers = [
37
        'scssphp', // https://github.com/leafo/scssphp
38
    ];
39
40
    /**
41
     * scssphp compiler
42
     * @link https://github.com/leafo/scssphp
43
     *
44
     * @param string $file
45
     *
46
     * @return string
47
     */
48
    protected function scssphp($file)
49
    {
50
        if (!class_exists('\Leafo\ScssPhp\Compiler')) {
51
            return Result::errorMissingPackage($this, 'scssphp', 'leafo/scssphp');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Robo\Result::err...php', 'leafo/scssphp'); (Robo\Result) is incompatible with the return type documented by Robo\Task\Assets\Scss::scssphp 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...
52
        }
53
54
        $scssCode = file_get_contents($file);
55
        $scss = new \Leafo\ScssPhp\Compiler();
56
57
        // set options for the scssphp compiler
58
        if (isset($this->compilerOptions['importDirs'])) {
59
            $scss->setImportPaths($this->compilerOptions['importDirs']);
60
        }
61
62
        if (isset($this->compilerOptions['formatter'])) {
63
            $scss->setFormatter($this->compilerOptions['formatter']);
64
        }
65
66
        return $scss->compile($scssCode);
67
    }
68
69
    /**
70
     * Sets the formatter for scssphp
71
     *
72
     * The method setFormatter($formatterName) sets the current formatter to $formatterName,
73
     * the name of a class as a string that implements the formatting interface. See the source
74
     * for Leafo\ScssPhp\Formatter\Expanded for an example.
75
     *
76
     * Five formatters are included with leafo/scssphp:
77
     * - Leafo\ScssPhp\Formatter\Expanded
78
     * - Leafo\ScssPhp\Formatter\Nested (default)
79
     * - Leafo\ScssPhp\Formatter\Compressed
80
     * - Leafo\ScssPhp\Formatter\Compact
81
     * - Leafo\ScssPhp\Formatter\Crunched
82
     *
83
     * @link http://leafo.github.io/scssphp/docs/#output-formatting
84
     *
85
     * @param string $formatterName
86
     *
87
     * @return $this
88
     */
89
    public function setFormatter($formatterName)
90
    {
91
        return parent::setFormatter($formatterName);
92
    }
93
}
94