Passed
Pull Request — master (#17)
by Mihail
15:10
created

ValidatableTrait.php (2 issues)

1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Http;
14
15
use Koded\Http\Interfaces\{HttpInputValidator, HttpStatus, Response};
16
use Koded\Stdlib\{Data, Immutable};
17
18
/**
19
 * @method Response|null getParsedBody
20
 */
21
trait ValidatableTrait
22
{
23
    public function validate(HttpInputValidator $validator, ?Data &$input = null): ?Response
24 4
    {
25
        $input ??= new Immutable($this->getParsedBody() ?? []);
0 ignored issues
show
It seems like $this->getParsedBody() ?? array() can also be of type Koded\Http\Interfaces\Response; however, parameter $data of Koded\Stdlib\Immutable::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        $input ??= new Immutable(/** @scrutinizer ignore-type */ $this->getParsedBody() ?? []);
Loading history...
26 4
        if (0 === $input->count()) {
0 ignored issues
show
The method count() does not exist on Koded\Stdlib\Data. It seems like you code against a sub-type of said class. However, the method does not exist in Koded\Stdlib\Argument or Koded\Stdlib\Configuration. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        if (0 === $input->/** @scrutinizer ignore-call */ count()) {
Loading history...
27
            $errors = ['validate' => 'Nothing to validate', 'code' => HttpStatus::BAD_REQUEST];
28 4
            return new JsonResponse($errors, HttpStatus::BAD_REQUEST, [
29 1
                'Content-Type' => 'application/problem+json'
30
            ]);
31
        }
32 3
        if (empty($errors = $validator->validate($input))) {
33 1
            return null;
34
        }
35
        $errors['status'] = (int)($errors['status'] ?? HttpStatus::BAD_REQUEST);
36 2
        return new JsonResponse($errors, $errors['status'], [
37 2
            'Content-Type' => 'application/problem+json'
38
        ]);
39
    }
40
}
41