Passed
Push — master ( 2cda12...3e0ae5 )
by Melech
02:04 queued 37s
created

RequestStruct   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDataFromRequest() 0 3 1
A validate() 0 3 1
A getValidationRules() 0 3 1
A determineIfRequestContainsExtraData() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Struct\Request\Trait;
15
16
use Valkyrja\Http\Message\Request\Contract\ServerRequest;
17
use Valkyrja\Type\BuiltIn\Enum\Trait\Arrayable;
18
use Valkyrja\Validation\Validator\Contract\Validator as ValidatorContract;
19
use Valkyrja\Validation\Validator\Validator;
20
21
/**
22
 * Trait RequestStruct.
23
 *
24
 * @author Melech Mizrachi
25
 */
26
trait RequestStruct
27
{
28
    use Arrayable;
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public static function getDataFromRequest(ServerRequest $request): array
34
    {
35
        return static::getOnlyParamsFromRequest($request, ...static::values());
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public static function determineIfRequestContainsExtraData(ServerRequest $request): bool
42
    {
43
        return ! empty(static::getExceptParamsFromRequest($request, ...static::values()));
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public static function getValidationRules(ServerRequest $request): array|null
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

49
    public static function getValidationRules(/** @scrutinizer ignore-unused */ ServerRequest $request): array|null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        return null;
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public static function validate(ServerRequest $request): ValidatorContract
58
    {
59
        return new Validator(static::getValidationRules($request) ?? []);
0 ignored issues
show
Bug introduced by
Are you sure the usage of static::getValidationRules($request) targeting Valkyrja\Http\Struct\Req...t::getValidationRules() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
60
    }
61
62
    /**
63
     * Get only the specified request params.
64
     *
65
     * @param ServerRequest $request   The request
66
     * @param string|int    ...$values The values
67
     *
68
     * @return array
69
     */
70
    abstract protected static function getOnlyParamsFromRequest(ServerRequest $request, string|int ...$values): array;
71
72
    /**
73
     * Get all request params except the ones specified.
74
     *
75
     * @param ServerRequest $request   The request
76
     * @param string|int    ...$values The values
77
     *
78
     * @return array
79
     */
80
    abstract protected static function getExceptParamsFromRequest(ServerRequest $request, string|int ...$values): array;
81
}
82