Passed
Push — master ( f5fadc...72d313 )
by Christian
02:28
created

AbstractFormHandler::handle()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 8
nop 3
dl 0
loc 35
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Christian Gripp <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core23\Form\Handler;
13
14
use Core23\Form\Handler\Exception\InvalidCallbackException;
15
use Symfony\Component\Form\FormInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
19
abstract class AbstractFormHandler implements FormHandlerInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    final public function handle(FormInterface $form, Request $request, callable $callback): ?Response
25
    {
26
        if ($response = $this->preProcess($form, $request)) {
0 ignored issues
show
Bug introduced by core23
Are you sure the assignment to $response is correct as $this->preProcess($form, $request) targeting Core23\Form\Handler\Abst...rmHandler::preProcess() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
27
            return $response;
0 ignored issues
show
Bug Best Practice introduced by core23
The expression return $response returns the type void which is incompatible with the type-hinted return null|Symfony\Component\HttpFoundation\Response.
Loading history...
28
        }
29
30
        $form->handleRequest($request);
31
32
        if (!$form->isSubmitted() || !$form->isValid()) {
33
            return null;
34
        }
35
36
        if ($response = $this->validate($form, $request)) {
0 ignored issues
show
Bug introduced by core23
Are you sure the assignment to $response is correct as $this->validate($form, $request) targeting Core23\Form\Handler\Abst...FormHandler::validate() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
37
            return $response;
0 ignored issues
show
Bug Best Practice introduced by core23
The expression return $response returns the type void which is incompatible with the type-hinted return null|Symfony\Component\HttpFoundation\Response.
Loading history...
38
        }
39
40
        if (!$this->process($form, $request)) {
41
            return null;
42
        }
43
44
        if (!\is_callable($callback)) {
45
            throw new InvalidCallbackException('No valid callable.');
46
        }
47
48
        $response = $callback();
49
50
        if (!$response instanceof Response) {
51
            throw new InvalidCallbackException('Invalid callback response.');
52
        }
53
54
        if ($response = $this->postProcess($form, $request, $response)) {
55
            return $response;
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * Executes before form validating and processing is started.
63
     *
64
     * @param FormInterface $form
65
     * @param Request       $request
66
     *
67
     * @return Response|null
68
     */
69
    protected function preProcess(FormInterface $form, Request $request): ?Response
0 ignored issues
show
Unused Code introduced by core23
The parameter $form 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

69
    protected function preProcess(/** @scrutinizer ignore-unused */ FormInterface $form, Request $request): ?Response

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...
Unused Code introduced by core23
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

69
    protected function preProcess(FormInterface $form, /** @scrutinizer ignore-unused */ Request $request): ?Response

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...
70
    {
71
        return null;
72
    }
73
74
    /**
75
     * Executes after preprocessing and before form processing is started.
76
     *
77
     * @param FormInterface $form
78
     * @param Request       $request
79
     *
80
     * @return Response|null
81
     */
82
    protected function validate(FormInterface $form, Request $request): ?Response
0 ignored issues
show
Unused Code introduced by core23
The parameter $form 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

82
    protected function validate(/** @scrutinizer ignore-unused */ FormInterface $form, Request $request): ?Response

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...
Unused Code introduced by core23
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

82
    protected function validate(FormInterface $form, /** @scrutinizer ignore-unused */ Request $request): ?Response

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...
83
    {
84
        return null;
85
    }
86
87
    /**
88
     * Executes the form processing.
89
     *
90
     * @param FormInterface $form
91
     * @param Request       $request
92
     *
93
     * @return bool
94
     */
95
    abstract protected function process(FormInterface $form, Request $request): bool;
96
97
    /**
98
     * Executes after form processing is finished and filters response.
99
     *
100
     * @param FormInterface $form
101
     * @param Request       $request
102
     * @param Response      $response
103
     *
104
     * @return Response|null
105
     */
106
    protected function postProcess(FormInterface $form, Request $request, Response $response): ?Response
0 ignored issues
show
Unused Code introduced by core23
The parameter $form 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

106
    protected function postProcess(/** @scrutinizer ignore-unused */ FormInterface $form, Request $request, Response $response): ?Response

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...
Unused Code introduced by core23
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

106
    protected function postProcess(FormInterface $form, /** @scrutinizer ignore-unused */ Request $request, Response $response): ?Response

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...
107
    {
108
        return $response;
109
    }
110
}
111