Completed
Push — master ( 22a756...a8d83f )
by John
02:03
created

src/ResponseBodyDehydrator.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\PhpApi\Middleware;
9
10
use Interop\Http\ServerMiddleware\DelegateInterface;
11
use KleijnWeb\PhpApi\Middleware\Body\BodySerializer;
12
use KleijnWeb\PhpApi\Middleware\Util\PhpApiMiddleware;
13
use KleijnWeb\PhpApi\Hydrator\Hydrator;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
class ResponseBodyDehydrator extends PhpApiMiddleware
18
{
19
    /**
20
     * @var Hydrator
21
     */
22
    private $hydrator;
23
24
    /**
25
     * @var BodySerializer|OkStatusResolver
26
     */
27
    private $okStatusResolver;
28
29
    /**
30
     * @param Hydrator              $hydrator
31
     * @param OkStatusResolver|null $okStatusResolver
32
     */
33
    public function __construct(Hydrator $hydrator, OkStatusResolver $okStatusResolver = null)
34
    {
35
        $this->hydrator         = $hydrator;
36
        $this->okStatusResolver = $okStatusResolver ?: new OkStatusResolver();
37
    }
38
39
    /**
40
     * Process a server request and return a response.
41
     *
42
     * @param ServerRequestInterface $request
43
     * @param DelegateInterface      $delegate
44
     *
45
     * @return ResponseInterface
46
     */
47
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
48
    {
49
        if (null !== ($data = $request->getAttribute(CommandDispatcher::ATTRIBUTE))) {
50
            $operation = $this->getOperation($request);
51
            $schema    = $operation
52
                ->getResponse($this->okStatusResolver->resolve($data, $operation))
0 ignored issues
show
The method resolve does only exist in KleijnWeb\PhpApi\Middleware\OkStatusResolver, but not in KleijnWeb\PhpApi\Middleware\Body\BodySerializer.

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...
53
                ->getSchema();
54
55
            $request = $request->withAttribute(
56
                CommandDispatcher::ATTRIBUTE,
57
                $this->hydrator->dehydrate($data, $schema)
58
            );
59
        }
60
61
        return $delegate->process($request);
62
    }
63
}