Test Failed
Push — dependency-injection ( 7565fa )
by MusikAnimal
07:05
created

BlameController::getIndexRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Model\Authorship;
8
use App\Model\Blame;
9
use App\Repository\BlameRepository;
10
use App\Repository\EditRepository;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
/**
15
 * This controller provides the search form and results page for the Blame tool.
16
 * @codeCoverageIgnore
17
 */
18
class BlameController extends XtoolsController
19
{
20
    /**
21
     * Get the name of the tool's index route. This is also the name of the associated model.
22
     * @return string
23
     * @codeCoverageIgnore
24
     */
25
    public function getIndexRoute(): string
26
    {
27
        return 'Blame';
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public function supportedProjects(): array
34
    {
35
        return Authorship::SUPPORTED_PROJECTS;
36
    }
37
38
    /**
39
     * The search form.
40
     * @Route("/blame", name="Blame")
41
     * @Route("/blame/{project}", name="BlameProject")
42
     * @return Response
43
     */
44
    public function indexAction(): Response
45
    {
46
        $this->params['target'] = $this->request->query->get('target', '');
47
48
        if (isset($this->params['project']) && isset($this->params['page']) && isset($this->params['q'])) {
49
            return $this->redirectToRoute('BlameResult', $this->params);
50
        }
51
52
        if (preg_match('/\d{4}-\d{2}-\d{2}/', $this->params['target'])) {
53
            $show = 'date';
54
        } elseif (is_numeric($this->params['target'])) {
55
            $show = 'id';
56
        } else {
57
            $show = 'latest';
58
        }
59
60
        return $this->render('blame/index.html.twig', array_merge([
61
            'xtPage' => 'Blame',
62
            'xtPageTitle' => 'tool-blame',
63
            'xtSubtitle' => 'tool-blame-desc',
64
            'project' => $this->project,
65
66
            // Defaults that will get overridden if in $params.
67
            'page' => '',
68
            'supportedProjects' => Authorship::SUPPORTED_PROJECTS,
69
        ], $this->params, [
70
            'project' => $this->project,
71
            'show' => $show,
72
            'target' => '',
73
        ]));
74
    }
75
76
    /**
77
     * @Route(
78
     *     "/blame/{project}/{page}/{target}",
79
     *     name="BlameResult",
80
     *     requirements={
81
     *         "page"="(.+?)",
82
     *         "target"="|latest|\d+|\d{4}-\d{2}-\d{2}",
83
     *     },
84
     *     defaults={"target"="latest"}
85
     * )
86
     * @param string $target
87
     * @param BlameRepository $blameRepo
88
     * @param EditRepository $editRepo
89
     * @return Response
90
     */
91
    public function resultAction(string $target, BlameRepository $blameRepo, EditRepository $editRepo): Response
92
    {
93
        if (!isset($this->params['q'])) {
94
            return $this->redirectToRoute('BlameProject', [
95
                'project' => $this->project->getDomain(),
96
            ]);
97
        }
98
        if (0 !== $this->page->getNamespace()) {
0 ignored issues
show
Bug introduced by
The method getNamespace() does not exist on null. ( Ignorable by Annotation )

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

98
        if (0 !== $this->page->/** @scrutinizer ignore-call */ getNamespace()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
            $this->addFlashMessage('danger', 'error-authorship-non-mainspace');
100
            return $this->redirectToRoute('BlameProject', [
101
                'project' => $this->project->getDomain(),
102
            ]);
103
        }
104
105
        // This action sometimes requires more memory. 256M should be safe.
106
        ini_set('memory_limit', '256M');
107
108
        $blame = new Blame($blameRepo, $editRepo, $this->page, $this->params['q'], $target);
0 ignored issues
show
Unused Code introduced by
The call to App\Model\Blame::__construct() has too many arguments starting with $target. ( Ignorable by Annotation )

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

108
        $blame = /** @scrutinizer ignore-call */ new Blame($blameRepo, $editRepo, $this->page, $this->params['q'], $target);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
$this->page of type App\Model\Page|null is incompatible with the type string expected by parameter $query of App\Model\Blame::__construct(). ( Ignorable by Annotation )

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

108
        $blame = new Blame($blameRepo, $editRepo, /** @scrutinizer ignore-type */ $this->page, $this->params['q'], $target);
Loading history...
Bug introduced by
$editRepo of type App\Repository\EditRepository is incompatible with the type App\Model\Page expected by parameter $page of App\Model\Blame::__construct(). ( Ignorable by Annotation )

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

108
        $blame = new Blame($blameRepo, /** @scrutinizer ignore-type */ $editRepo, $this->page, $this->params['q'], $target);
Loading history...
109
        $blame->setRepository($blameRepo);
110
        $blame->prepareData();
111
112
        return $this->getFormattedResponse('blame/blame', [
113
            'xtPage' => 'Blame',
114
            'xtTitle' => $this->page->getTitle(),
115
            'blame' => $blame,
116
        ]);
117
    }
118
}
119