Passed
Push — main ( 50b4db...a7edb3 )
by MusikAnimal
03:21
created

LargestPagesRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 8
dl 0
loc 12
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace App\Repository;
6
7
use App\Model\Page;
8
use App\Model\Project;
9
use Doctrine\Persistence\ManagerRegistry;
10
use GuzzleHttp\Client;
11
use Psr\Cache\CacheItemPoolInterface;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
14
15
/**
16
 * A LargestPagesRepository is responsible for retrieving information from the database for the LargestPages tool.
17
 * @codeCoverageIgnore
18
 */
19
class LargestPagesRepository extends Repository
20
{
21
    protected PageRepository $pageRepo;
22
23
    /**
24
     * @param ManagerRegistry $managerRegistry
25
     * @param CacheItemPoolInterface $cache
26
     * @param Client $guzzle
27
     * @param LoggerInterface $logger
28
     * @param ParameterBagInterface $parameterBag
29
     * @param bool $isWMF
30
     * @param int $queryTimeout
31
     * @param PageRepository $pageRepo
32
     */
33
    public function __construct(
34
        ManagerRegistry $managerRegistry,
35
        CacheItemPoolInterface $cache,
36
        Client $guzzle,
37
        LoggerInterface $logger,
38
        ParameterBagInterface $parameterBag,
39
        bool $isWMF,
40
        int $queryTimeout,
41
        PageRepository $pageRepo
42
    ) {
43
        $this->pageRepo = $pageRepo;
44
        parent::__construct($managerRegistry, $cache, $guzzle, $logger, $parameterBag, $isWMF, $queryTimeout);
45
    }
46
47
    /** @var int Max rows to display. */
48
    public const MAX_ROWS = 500;
49
50
    private function getLikeSql(string &$includePattern, string &$excludePattern): string
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_VARIABLE, expecting T_STRING or T_NAME_QUALIFIED or T_NAME_FULLY_QUALIFIED or T_NAME_RELATIVE on line 50 at column 40
Loading history...
51
    {
52
        $sql = '';
53
54
        if ($includePattern) {
55
            $sql .= "page_title LIKE :include_pattern ";
56
            $includePattern = str_replace(' ', '_', $includePattern);
57
        }
58
        if ($excludePattern) {
59
            if ($includePattern) {
60
                $sql .= ' AND ';
61
            }
62
            $sql .= "page_title NOT LIKE :exclude_pattern ";
63
            $excludePattern = str_replace(' ', '_', $excludePattern);
64
        }
65
66
        return $sql;
67
    }
68
69
    /**
70
     * Fetches the largest pages for the given project.
71
     * @param Project $project
72
     * @param int|string $namespace Namespace ID or 'all' for all namespaces.
73
     * @param string $includePattern Either regular expression (starts/ends with forward slash),
74
     *   or a wildcard pattern with % as the wildcard symbol.
75
     * @param string $excludePattern Either regular expression (starts/ends with forward slash),
76
     *   or a wildcard pattern with % as the wildcard symbol.
77
     * @return array
78
     */
79
    public function getData(Project $project, $namespace, string $includePattern, string $excludePattern): array
80
    {
81
        $pageTable = $project->getTableName('page');
82
83
        $where = '';
84
        $likeCond = $this->getLikeSql($includePattern, $excludePattern);
85
        $namespaceCond = '';
86
        if ('all' !== $namespace) {
87
            $namespaceCond = 'page_namespace = :namespace';
88
            if ($likeCond) {
89
                $namespaceCond .= ' AND ';
90
            }
91
        }
92
        if ($likeCond || $namespaceCond) {
93
            $where = 'WHERE ';
94
        }
95
96
        $sql = "SELECT page_namespace AS `namespace`, page_title, page_len AS `length`
97
                FROM $pageTable
98
                $where $namespaceCond
99
                $likeCond
100
                ORDER BY page_len DESC
101
                LIMIT ".self::MAX_ROWS;
102
103
        $rows = $this->executeProjectsQuery($project, $sql, [
104
            'namespace' => $namespace,
105
            'include_pattern' => $includePattern,
106
            'exclude_pattern' => $excludePattern,
107
        ])->fetchAllAssociative();
108
109
        $pages = [];
110
111
        foreach ($rows as $row) {
112
            $pages[] = Page::newFromRow($this->pageRepo, $project, $row);
113
        }
114
115
        return $pages;
116
    }
117
}
118