Completed
Push — master ( 84e322...4c12f0 )
by Charis
02:20
created

Uri::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Resilient\Twig\Extension;
4
5
use \Zend\Diactoros\Uri;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Resilient\Twig\Extension\Uri.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
/**
8
* Twig Router Extension for zendframework/zend-diactoros
9
*/
10
class Uri extends \Twig_Extension
11
{
12
    private $uri;
13
14
    public function __construct(Uri $uri)
15
    {
16
        $this->uri = $uri;
17
    }
18
19
    public function getFunctions()
20
    {
21
        return [
22
            new \Twig_SimpleFunction('base_url', array($this, 'baseUrl')),
23
            new \Twig_SimpleFunction('current_path', array($this, 'currentPath')),
24
            new \Twig_SimpleFunction('current_url', array($this, 'currentUrl')),
25
        ];
26
    }
27
28
    /**
29
     * baseUrl function.
30
     *
31
     * @access public
32
     * @return string uri
33
     */
34
    public function baseUrl()
35
    {
36
        if (is_string($this->uri)) {
37
            return $this->uri;
38
        }
39
        if (method_exists($this->uri, 'getBaseUrl')) {
40
            return $this->uri->getBaseUrl();
0 ignored issues
show
Bug introduced by
The method getBaseUrl() does not seem to exist on object<Zend\Diactoros\Uri>.

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...
41
        }
42
    }
43
44
    /**
45
     * currentPath function.
46
     *
47
     * @access public
48
     * @return string
49
     */
50
    public function currentPath()
51
    {
52
        return $this->uri->getPath();
53
    }
54
55
    /**
56
     * currentUrl function.
57
     *
58
     * @access public
59
     * @return string
60
     */
61
    public function currentUrl()
62
    {
63
        return (string) $this->uri;
64
    }
65
}
66