Passed
Pull Request — master (#91)
by Alexander
09:37 queued 07:29
created

FallbackGenerator::generateAbsolute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
class FallbackGenerator implements UrlGeneratorInterface
8
{
9
    private UrlGeneratorInterface $urlGenerator;
10
11
    /**
12
     * FallbackGenerator constructor.
13
     */
14
    public function __construct(UrlGeneratorInterface $urlGenerator)
15
    {
16
        $this->urlGenerator = $urlGenerator;
17
    }
18
19
    public function generate(string $name, array $parameters = []): string
20
    {
21
        try {
22
            return $this->urlGenerator->generate($name, $parameters);
23
        } catch (RouteNotFoundException $e) {
24
            // generate fallback URL
25
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
26
    }
27
28
    public function generateAbsolute(string $name, array $parameters = [], string $scheme = null, string $host = null): string
29
    {
30
        try {
31
            return $this->urlGenerator->generateAbsolute($name, $parameters, $scheme, $host);
32
        } catch (RouteNotFoundException $e) {
33
            // generate fallback URL
34
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
35
    }
36
37
    public function getUriPrefix(): string
38
    {
39
        return $this->urlGenerator->getUriPrefix();
40
    }
41
42
    public function setUriPrefix(string $name): void
43
    {
44
        $this->urlGenerator->setUriPrefix($name);
45
    }
46
}
47