Completed
Push — master ( cee8b2...c54e9a )
by Charis
06:39
created

Slim   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 77
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getName() 0 4 1
A getFunctions() 0 8 1
A pathFor() 0 4 1
A baseUrl() 0 9 3
A isCurrentPath() 0 4 1
A setBaseUrl() 0 4 1
1
<?php
2
3
namespace Resilient\Twig\Extension;
4
5
use \Psr\Http\Message\UriInterface;
6
use \Slim\Interfaces\RouterInterface;
7
8
/**
9
* Twig Router Extension for Slim Routing
10
*/
11
class Slim extends \Twig_Extension
12
{
13
    private $uri;
14
15
    public function __construct(RouterInterface $router, UriInterface $uri)
16
    {
17
        $this->router = $router;
0 ignored issues
show
Bug introduced by
The property router does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
18
        $this->uri = $uri;
19
    }
20
21
    public function getName()
22
    {
23
        return 'slim kln';
24
    }
25
26
    public function getFunctions()
27
    {
28
        return [
29
            new \Twig_SimpleFunction('path_for', array($this, 'pathFor')),
30
            new \Twig_SimpleFunction('base_url', array($this, 'baseUrl')),
31
            new \Twig_SimpleFunction('is_current_path', array($this, 'isCurrentPath')),
32
        ];
33
    }
34
35
    /**
36
     * pathFor function.
37
     *
38
     * @access public
39
     * @param mixed $name
40
     * @param mixed $data (default: [])
41
     * @param mixed $queryParams (default: [])
42
     * @return string
43
     */
44
    public function pathFor($name, $data = [], $queryParams = [])
45
    {
46
        return $this->router->pathFor($name, $data, $queryParams);
47
    }
48
49
    /**
50
     * baseUrl function.
51
     *
52
     * @access public
53
     * @return string
54
     */
55
    public function baseUrl()
56
    {
57
        if (is_string($this->uri)) {
58
            return $this->uri;
59
        }
60
        if (method_exists($this->uri, 'getBaseUrl')) {
61
            return $this->uri->getBaseUrl();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\UriInterface as the method getBaseUrl() does only exist in the following implementations of said interface: Slim\Http\Uri.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
62
        }
63
    }
64
65
    /**
66
     * isCurrentPath function.
67
     *
68
     * @access public
69
     * @param mixed $name
70
     * @return boolean
71
     */
72
    public function isCurrentPath($name)
73
    {
74
        return $this->router->pathFor($name) === $this->uri->getPath();
75
    }
76
77
    /**
78
     * Set the base url
79
     *
80
     * @param string|Slim\Http\Uri $baseUrl
81
     * @return void
82
     */
83
    public function setBaseUrl($baseUrl)
84
    {
85
        $this->uri = $baseUrl;
86
    }
87
}
88