SlugRoute::match()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
namespace App\Routing\Route;
3
4
use Cake\Routing\Route\Route;
5
use Cake\Utility\Inflector;
6
7
class SlugRoute extends Route
8
{
9
10
    /**
11
     * Flag for tracking whether or not the defaults have been sluged.
12
     *
13
     * Default values need to be sluged so that they match the inflections that
14
     * match() will create.
15
     *
16
     * @var bool
17
     */
18
    protected $_slugedDefaults = false;
19
20
    /**
21
     * Parses a string URL into an array. If it matches, it will convert the slug keys to their slugerize form
22
     *
23
     * @param string $url The URL to parse.
24
     * @param string $method The HTTP method.
25
     *
26
     * @return false|array False on failure, or an array of request parameters.
27
     */
28
    public function parse($url, $method = '')
29
    {
30
        $params = parent::parse($url, $method);
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Routing\Route\Route::parse() has been deprecated with message: 3.4.0 Use/implement parseRequest() instead as it provides more flexibility/control.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
31
        if (!$params) {
32
            return false;
33
        }
34
        if (!empty($params['slug'])) {
35
            $params['slug'] = strtolower(Inflector::slug($params['slug']));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Utility\Inflector::slug() has been deprecated with message: 3.2.7 Use Text::slug() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
36
        }
37
        if (!empty($params['controller'])) {
38
            $params['controller'] = Inflector::camelize($params['controller']);
39
        }
40
        if (!empty($params['plugin'])) {
41
            $params['plugin'] = Inflector::camelize($params['plugin']);
42
        }
43
44
        return $params;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $params; (array) is incompatible with the return type of the parent method Cake\Routing\Route\Route::parse of type false|array.

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...
45
    }
46
47
    /**
48
     * Slug the prefix, controller and plugin params before passing them on to the
49
     * parent class
50
     *
51
     * @param array $url Array of parameters to convert to a string.
52
     * @param array $context An array of the current request context.
53
     *   Contains information such as the current host, scheme, port, and base
54
     *   directory.
55
     *
56
     * @return false|string Either false or a string URL.
57
     */
58
    public function match(array $url, array $context = [])
59
    {
60
        $url = $this->_slugerize($url);
61
        if (!$this->_slugedDefaults) {
62
            $this->_slugedDefaults = true;
63
            $this->defaults = $this->_slugerize($this->defaults);
64
        }
65
66
        return parent::match($url, $context);
67
    }
68
69
    /**
70
     * Helper method for slugerizing keys in a URL array.
71
     *
72
     * @param array $url An array of URL keys.
73
     *
74
     * @return array
75
     */
76
    protected function _slugerize($url)
77
    {
78
        if (isset($url['slug']) && !empty($url['slug'])) {
79
            $url['slug'] = strtolower(Inflector::slug($url['slug']));
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Utility\Inflector::slug() has been deprecated with message: 3.2.7 Use Text::slug() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
        }
81
        if (!empty($url['controller'])) {
82
            $url['controller'] = Inflector::underscore($url['controller']);
83
        }
84
        if (!empty($url['plugin'])) {
85
            $url['plugin'] = Inflector::underscore($url['plugin']);
86
        }
87
88
        return $url;
89
    }
90
}
91