Passed
Pull Request — master (#91)
by Alexander
07:39
created

FallbackGenerator::setUriPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
8
class FallbackGenerator implements UrlGeneratorInterface
9
{
10
    private UrlGeneratorInterface $urlGenerator;
11
12
    /**
13
     * FallbackGenerator constructor.
14
     */
15
    public function __construct(UrlGeneratorInterface $urlGenerator)
16
    {
17
        $this->urlGenerator = $urlGenerator;
18
    }
19
20
    public function generate(string $name, array $parameters = []): string
21
    {
22
        try {
23
            return $this->urlGenerator->generate($name, $parameters);
24
        } catch (RouteNotFoundException $e) {
25
            // generate fallback URL
26
        }
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...
27
    }
28
29
    public function generateAbsolute(string $name, array $parameters = [], string $scheme = null, string $host = null): string
30
    {
31
        try {
32
            return $this->urlGenerator->generateAbsolute($name, $parameters, $scheme, $host);
33
        } catch (RouteNotFoundException $e) {
34
            // generate fallback URL
35
        }
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...
36
    }
37
38
    public function getUriPrefix(): string
39
    {
40
        return $this->urlGenerator->getUriPrefix();
41
    }
42
43
    public function setUriPrefix(string $name): void
44
    {
45
        $this->urlGenerator->setUriPrefix($name);
46
    }
47
}
48