LanguageMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 53
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 10 2
A matchLanguageFromPath() 0 9 1
A matchLanguageFromParams() 0 5 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Acelaya\Website\Middleware;
5
6
use Interop\Http\ServerMiddleware\DelegateInterface;
7
use Interop\Http\ServerMiddleware\MiddlewareInterface;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Zend\Expressive\Router\RouteResult;
11
use Zend\Expressive\Router\RouterInterface;
12
use Zend\I18n\Translator\Translator;
13
use Zend\I18n\Translator\TranslatorInterface;
14
15
class LanguageMiddleware implements MiddlewareInterface
16
{
17
    /**
18
     * @var TranslatorInterface|Translator
19
     */
20
    protected $translator;
21
    /**
22
     * @var RouterInterface
23
     */
24
    protected $router;
25
26
    public function __construct(TranslatorInterface $translator, RouterInterface $router)
27
    {
28
        $this->translator = $translator;
29 1
        $this->router = $router;
30
    }
31 1
32 1
    /**
33 1
     * Process an incoming server request and return a response, optionally delegating
34
     * to the next middleware component to create the response.
35
     *
36
     * @param Request $request
37
     * @param DelegateInterface $delegate
38
     *
39
     * @return Response
40
     */
41
    public function process(Request $request, DelegateInterface $delegate)
42
    {
43
        $matchedRoute = $this->router->match($request);
44
        $lang = $matchedRoute->isFailure()
45
            ? $this->matchLanguageFromPath($request)
46
            : $this->matchLanguageFromParams($matchedRoute);
47
48
        $this->translator->setLocale($lang);
0 ignored issues
show
Bug introduced by
The method setLocale does only exist in Zend\I18n\Translator\Translator, but not in Zend\I18n\Translator\TranslatorInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
        return $delegate->process($request);
50
    }
51
52
    private function matchLanguageFromPath(Request $request): string
53
    {
54
        $path = $request->getUri()->getPath();
55
        $parts = array_filter(explode('/', $path), function (string $value) {
56
            return ! empty($value);
57
        });
58
        $langPart = strtolower(array_shift($parts) ?? '');
59
        return $langPart;
60 1
    }
61
62 1
    private function matchLanguageFromParams(RouteResult $routeResult): string
63 1
    {
64
        $params = $routeResult->getMatchedParams();
65
        return $params['lang'] ?? 'en';
66 1
    }
67
}
68