TwigExtension::baseUrl()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the jade/jade package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jade\Twig;
13
14
use Jade\Routing\Router;
15
use Psr\Http\Message\UriInterface;
16
17
class TwigExtension extends \Twig\Extension\AbstractExtension
18
{
19
    /**
20
     * @var Router
21
     */
22
    private $router;
23
    /**
24
     * @var string|UriInterface
25
     */
26
    private $uri;
27
28
    public function __construct($router, $uri)
29
    {
30
        $this->router = $router;
31
        $this->uri = $uri;
32
    }
33
34
    public function getName()
35
    {
36
        return 'slim';
37
    }
38
39
    public function getFunctions()
40
    {
41
        return [
42
            new \Twig\TwigFunction('path_for', array($this, 'pathFor')),
43
            new \Twig\TwigFunction('full_url_for', array($this, 'fullUrlFor')),
44
            new \Twig\TwigFunction('base_url', array($this, 'baseUrl')),
45
            new \Twig\TwigFunction('is_current_path', array($this, 'isCurrentPath')),
46
            new \Twig\TwigFunction('current_path', array($this, 'currentPath')),
47
        ];
48
    }
49
50
    public function pathFor($name, $data = [], $queryParams = [], $appName = 'default')
51
    {
52
        return $this->router->pathFor($name, $data, $queryParams);
0 ignored issues
show
Bug introduced by
The method pathFor() does not seem to exist on object<Jade\Routing\Router>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
    }
54
55
    /**
56
     * Similar to pathFor but returns a fully qualified URL
57
     *
58
     * @param string $name The name of the route
59
     * @param array $data Route placeholders
60
     * @param array $queryParams
61
     * @param string $appName
62
     * @return string fully qualified URL
63
     */
64
    public function fullUrlFor($name, $data = [], $queryParams = [], $appName = 'default')
65
    {
66
        $path = $this->pathFor($name, $data, $queryParams, $appName);
67
        /** @var Uri $uri */
68
        if (is_string($this->uri)) {
69
            $uri = Uri::createFromString($this->uri);
70
        } else {
71
            $uri = $this->uri;
72
        }
73
        $scheme = $uri->getScheme();
74
        $authority = $uri->getAuthority();
75
        $host = ($scheme ? $scheme . ':' : '')
76
            . ($authority ? '//' . $authority : '');
77
        return $host . $path;
78
    }
79
80
    public function baseUrl()
81
    {
82
        if (is_string($this->uri)) {
83
            return $this->uri;
84
        }
85
        if (method_exists($this->uri, 'getBaseUrl')) {
86
            return $this->uri->getBaseUrl();
0 ignored issues
show
Bug introduced by
The method getBaseUrl() does not seem to exist on object<Psr\Http\Message\UriInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
        }
88
    }
89
90
    public function isCurrentPath($name, $data = [])
91
    {
92
        return $this->router->pathFor($name, $data) === $this->uri->getBasePath() . '/' . ltrim($this->uri->getPath(), '/');
0 ignored issues
show
Bug introduced by
The method pathFor() does not seem to exist on object<Jade\Routing\Router>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getBasePath() does not seem to exist on object<Psr\Http\Message\UriInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
    }
94
95
    /**
96
     * Returns current path on given URI.
97
     *
98
     * @param bool $withQueryString
99
     * @return string
100
     */
101
    public function currentPath($withQueryString = false)
102
    {
103
        if (is_string($this->uri)) {
104
            return $this->uri;
105
        }
106
        $path = $this->uri->getBasePath() . '/' . ltrim($this->uri->getPath(), '/');
0 ignored issues
show
Bug introduced by
The method getBasePath() does not seem to exist on object<Psr\Http\Message\UriInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        if ($withQueryString && '' !== $query = $this->uri->getQuery()) {
108
            $path .= '?' . $query;
109
        }
110
        return $path;
111
    }
112
113
    /**
114
     * Set the base url
115
     *
116
     * @param string|Slim\Http\Uri $baseUrl
117
     * @return void
118
     */
119
    public function setBaseUrl($baseUrl)
120
    {
121
        $this->uri = $baseUrl;
0 ignored issues
show
Documentation Bug introduced by
It seems like $baseUrl can also be of type object<Jade\Twig\Slim\Http\Uri>. However, the property $uri is declared as type string|object<Psr\Http\Message\UriInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
122
    }
123
}