Completed
Push — master ( b9ff5d...bb6c13 )
by Cheren
02:54
created

UrlHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C assetUrl() 0 40 14
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\View\Helper;
17
18
use Cake\Routing\Router;
19
use Cake\Utility\Inflector;
20
use Cake\View\Helper\UrlHelper as CakeUrlHelper;
21
22
/**
23
 * Class UrlHelper
24
 *
25
 * @package Core\View\Helper
26
 */
27
class UrlHelper extends CakeUrlHelper
28
{
29
    public function assetUrl($path, array $options = [])
30
    {
31
        if (is_array($path)) {
32
            return $this->build($path, !empty($options['fullBase']));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->build($pat...$options['fullBase'])); (array|boolean|string) is incompatible with the return type of the parent method Cake\View\Helper\UrlHelper::assetUrl 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...
33
        }
34
        if (strpos($path, '://') !== false || preg_match('/^[a-z]+:/i', $path)) {
35
            return $path;
36
        }
37
        if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
38
            list($plugin, $path) = $this->_View->pluginSplit($path, false);
39
        }
40
        if (!empty($options['pathPrefix']) && $path[0] !== '/') {
41
            $path = $options['pathPrefix'] . $path;
42
        }
43
        if (!empty($options['ext']) &&
44
            strpos($path, '?') === false &&
45
            substr($path, -strlen($options['ext'])) !== $options['ext']
46
        ) {
47
            $path .= $options['ext'];
48
        }
49
        if (preg_match('|^([a-z0-9]+:)?//|', $path)) {
50
            return $path;
51
        }
52
        dump($plugin);
0 ignored issues
show
Bug introduced by
The variable $plugin does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
53
        if (isset($plugin)) {
54
            dump($plugin);
55
            dump($path);
56
            $path = Inflector::underscore($plugin) . '/' . $path;
57
            dump($path);
58
        }
59
        dump($path);
60
        $path = $this->_encodeUrl($this->assetTimestamp($this->webroot($path)));
61
        dump($path);
62
63
        if (!empty($options['fullBase'])) {
64
            $path = rtrim(Router::fullBaseUrl(), '/') . '/' . ltrim($path, '/');
65
        }
66
67
        return $path;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $path; (array|boolean|string) is incompatible with the return type of the parent method Cake\View\Helper\UrlHelper::assetUrl 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...
68
    }
69
}
70