Less::compile()   A
last analyzed

Complexity

Conditions 5
Paths 14

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.1928
c 0
b 0
f 0
cc 5
nc 14
nop 2
1
<?php
2
/**
3
 * CakeCMS Core
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Core
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Core".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Core\Less;
17
18
use JBZoo\Utils\FS;
19
use JBZoo\Less\Exception;
20
use JBZoo\Less\Less as JBLess;
21
22
/**
23
 * Class Less
24
 *
25
 * @package Core\Lib\Less
26
 */
27
class Less extends JBLess
28
{
29
30
    /**
31
     * Compile less file.
32
     *
33
     * @param string $lessFile
34
     * @param null|string $basePath
35
     * @return array
36
     * @throws Exception
37
     */
38
    public function compile($lessFile, $basePath = null)
39
    {
40
        try {
41
            $basePath = $this->_prepareBasepath($basePath, dirname($lessFile));
42
43
            $cache = new Cache($this->_options);
44
            $cache->setFile($lessFile, $basePath);
45
46
            $isExpired = $cache->isExpired();
47
            $isForce   = $this->_options->get('force', false, 'bool');
48
49
            if ($isForce || $cache->isExpired()) {
50
                $result = $this->_driver->compile($lessFile, $basePath);
51
                $cache->save($result);
52
            }
53
54
            $isExpired = ($isForce) ? true : $isExpired;
55
            $return = [FS::clean($cache->getFile(), '/'), $isExpired];
56
57
        } catch (\Exception $e) {
58
            $message = 'Less error: ' . $e->getMessage();
59
            throw new Exception($message);
60
        }
61
62
        return $return;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $return; (array<string|boolean>) is incompatible with the return type of the parent method JBZoo\Less\Less::compile 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...
63
    }
64
}
65