Completed
Push — master ( 70dcd2...ca2130 )
by Andy
02:07
created

CanonicalUrlGenerator::generateUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 6
cts 9
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 2
crap 3.3332
1
<?php
2
3
namespace Palmtree\CanonicalUrlBundle\Service;
4
5
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6
use Symfony\Component\Routing\RouterInterface;
7
8
class CanonicalUrlGenerator
9
{
10
    /** @var RouterInterface */
11
    protected $router;
12
    /** @var string */
13
    protected $siteUrl;
14
    /** @var bool */
15
    protected $trailingSlash;
16
17
    /**
18
     * CanonicalUrlService constructor.
19
     * @param RouterInterface $router
20
     * @param array           $config
21
     */
22 5
    public function __construct(RouterInterface $router, array $config = [])
23
    {
24 5
        $this->router = $router;
25
26 5
        $this->siteUrl       = $config['site_url'];
27 5
        $this->trailingSlash = $config['trailing_slash'];
28 5
    }
29
30
    /**
31
     * Returns the canonical URL for a route.
32
     *
33
     * @param string       $route
34
     * @param array|string $parameters
35
     *
36
     * @return string
37
     */
38 3
    public function generateUrl($route, $parameters = [])
39
    {
40 3
        if (!$route) {
41
            return '';
42
        }
43
44 3
        $parameters = $this->getParameters($parameters);
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by $this->getParameters($parameters) on line 44 can also be of type string; however, Palmtree\CanonicalUrlBun...erator::getParameters() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
45
46
        try {
47 3
            $uri = $this->router->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH);
48
        } catch (\Exception $exception) {
49
            return '';
50
        }
51
52 3
        $url = rtrim($this->siteUrl, '/') . '/' . ltrim($uri, '/');
53
54 3
        return $url;
55
    }
56
57
    /**
58
     * @param array $parameters
59
     * @return array|string
60
     */
61 3
    protected function getParameters($parameters = [])
62
    {
63 3
        if (is_string($parameters)) {
64
            parse_str($parameters, $parameters);
65
        }
66
67 3
        if (!$parameters) {
68 3
            $parameters = [];
69
        }
70
71 3
        return $parameters;
72
    }
73
}
74