Completed
Push — master ( 909e64...7cd7e5 )
by Denis
03:34
created

ILess::_compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
/**
3
 * JBZoo Less
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   Less
10
 * @license   MIT
11
 * @copyright Copyright (C) JBZoo.com,  All rights reserved.
12
 * @link      https://github.com/JBZoo/Less
13
 * @author    Denis Smetannikov <[email protected]>
14
 */
15
16
namespace JBZoo\Less\Driver;
17
18
use ILess\Parser;
19
use JBZoo\Less\Exception;
20
use JBZoo\Utils\FS;
21
22
/**
23
 * Class Iless
24
 * @package JBZoo\Less
25
 * @codeCoverageIgnore
26
 */
27
class ILess extends Driver
28
{
29
    /**
30
     * @var Parser
31
     */
32
    protected $_compiler;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function _compile($fullPath, $relPath)
38
    {
39
        $this->_initCompiler();
40
41
        $this->_compiler->parseFile($fullPath);
42
43
        $resultCss = $this->_compiler->getCSS();
44
45
        return $resultCss;
46
    }
47
48
    /**
49
     * @return \Less_Parser
50
     */
51
    protected function _initCompiler()
52
    {
53
        if ($this->_compiler) {
54
            return $this->_compiler;
55
        }
56
57
        $this->_compiler = new Parser([
58
            'import_dirs' => $this->_options->get('import_paths'),
59
        ]);
60
61
        $this->_compiler->addVariables((array)$this->_options->get('global_vars', []));
62
63
        return $this->_compiler;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function setImportPath($fullPath, $relPath = null)
70
    {
71
        if (!FS::isDir($fullPath)) {
72
            throw new Exception('Undefined import path: ' . $fullPath);
73
        }
74
75
        return false;
0 ignored issues
show
Bug Best Practice introduced by smetdenis
The return type of return false; (false) is incompatible with the return type declared by the abstract method JBZoo\Less\Driver\Driver::setImportPath 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...
76
    }
77
}
78