| Total Complexity | 43 |
| Total Lines | 396 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ProjectRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProjectRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ProjectRepository extends Repository |
||
| 22 | { |
||
| 23 | /** @var array Project's 'dbName', 'url' and 'lang'. */ |
||
| 24 | protected $basicInfo; |
||
| 25 | |||
| 26 | /** @var string[] Basic metadata if XTools is in single-wiki mode. */ |
||
| 27 | protected $singleBasicInfo; |
||
| 28 | |||
| 29 | /** @var array Full Project metadata, including $basicInfo. */ |
||
| 30 | protected $metadata; |
||
| 31 | |||
| 32 | /** @var string The cache key for the 'all project' metadata. */ |
||
| 33 | protected $cacheKeyAllProjects = 'allprojects'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Convenience method to get a new Project object based on a given identification string. |
||
| 37 | * @param string $projectIdent The domain name, database name, or URL of a project. |
||
| 38 | * @param ContainerInterface $container Symfony's container. |
||
| 39 | * @return Project |
||
| 40 | */ |
||
| 41 | public static function getProject(string $projectIdent, ContainerInterface $container): Project |
||
| 42 | { |
||
| 43 | $project = new Project($projectIdent); |
||
| 44 | $projectRepo = new ProjectRepository(); |
||
| 45 | $projectRepo->setContainer($container); |
||
| 46 | |||
| 47 | // The associated PageAssessmentsRepository also needs the container. |
||
| 48 | $paRepo = new PageAssessmentsRepository(); |
||
| 49 | $paRepo->setContainer($container); |
||
| 50 | $project->getPageAssessments()->setRepository($paRepo); |
||
| 51 | |||
| 52 | if ($container->getParameter('app.single_wiki')) { |
||
| 53 | $projectRepo->setSingleBasicInfo([ |
||
| 54 | 'url' => $container->getParameter('wiki_url'), |
||
| 55 | 'dbName' => $container->getParameter('database_replica_name'), |
||
| 56 | ]); |
||
| 57 | } |
||
| 58 | $project->setRepository($projectRepo); |
||
| 59 | |||
| 60 | return $project; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the XTools default project. |
||
| 65 | * @param ContainerInterface $container |
||
| 66 | * @return Project |
||
| 67 | */ |
||
| 68 | public static function getDefaultProject(ContainerInterface $container): Project |
||
| 69 | { |
||
| 70 | $defaultProjectName = $container->getParameter('default_project'); |
||
| 71 | return self::getProject($defaultProjectName, $container); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get the global 'meta' project, which is either Meta (if this is Labs) or the default project. |
||
| 76 | * @return Project |
||
| 77 | */ |
||
| 78 | public function getGlobalProject(): Project |
||
| 79 | { |
||
| 80 | if ($this->isLabs()) { |
||
| 81 | return self::getProject('metawiki', $this->container); |
||
| 82 | } else { |
||
| 83 | return self::getDefaultProject($this->container); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * For single-wiki installations, you must manually set the wiki URL and database name |
||
| 89 | * (because there's no meta.wiki database to query). |
||
| 90 | * @param array $metadata |
||
| 91 | * @throws \Exception |
||
| 92 | */ |
||
| 93 | public function setSingleBasicInfo(array $metadata): void |
||
| 94 | { |
||
| 95 | if (!array_key_exists('url', $metadata) || !array_key_exists('dbName', $metadata)) { |
||
| 96 | $error = "Single-wiki metadata should contain 'url', 'dbName' and 'lang' keys."; |
||
| 97 | throw new \Exception($error); |
||
| 98 | } |
||
| 99 | $this->singleBasicInfo = array_intersect_key($metadata, [ |
||
| 100 | 'url' => '', |
||
| 101 | 'dbName' => '', |
||
| 102 | 'lang' => '', |
||
| 103 | ]); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the 'dbName', 'url' and 'lang' of all projects. |
||
| 108 | * @return string[][] Each item has 'dbName', 'url' and 'lang' keys. |
||
| 109 | */ |
||
| 110 | public function getAll(): array |
||
| 111 | { |
||
| 112 | $this->log->debug(__METHOD__." Getting all projects' metadata"); |
||
| 113 | // Single wiki mode? |
||
| 114 | if (!empty($this->singleBasicInfo)) { |
||
| 115 | return [$this->getOne('')]; |
||
| 116 | } |
||
| 117 | |||
| 118 | // Maybe we've already fetched it. |
||
| 119 | if ($this->cache->hasItem($this->cacheKeyAllProjects)) { |
||
| 120 | return $this->cache->getItem($this->cacheKeyAllProjects)->get(); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ($this->container->hasParameter("database_meta_table")) { |
||
| 124 | $table = $this->container->getParameter("database_meta_table"); |
||
| 125 | } else { |
||
| 126 | $table = "wiki"; |
||
| 127 | } |
||
| 128 | |||
| 129 | // Otherwise, fetch all from the database. |
||
| 130 | $wikiQuery = $this->getMetaConnection()->createQueryBuilder(); |
||
| 131 | $wikiQuery->select(['dbname AS dbName', 'url', 'lang'])->from($table); |
||
| 132 | $projects = $this->executeQueryBuilder($wikiQuery)->fetchAll(); |
||
| 133 | $projectsMetadata = []; |
||
| 134 | foreach ($projects as $project) { |
||
| 135 | $projectsMetadata[$project['dbName']] = $project; |
||
| 136 | } |
||
| 137 | |||
| 138 | // Cache for one day and return. |
||
| 139 | return $this->setCache( |
||
| 140 | $this->cacheKeyAllProjects, |
||
| 141 | $projectsMetadata, |
||
| 142 | 'P1D' |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the 'dbName', 'url' and 'lang' of a project. This is all you need to make database queries. |
||
| 148 | * More comprehensive metadata can be fetched with getMetadata() at the expense of an API call. |
||
| 149 | * @param string $project A project URL, domain name, or database name. |
||
| 150 | * @return string[]|bool With 'dbName', 'url' and 'lang' keys; or false if not found. |
||
| 151 | */ |
||
| 152 | public function getOne(string $project) |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get metadata about a project, including the 'dbName', 'url' and 'lang' |
||
| 213 | * |
||
| 214 | * @param string $projectUrl The project's URL. |
||
| 215 | * @return array|null With 'dbName', 'url', 'lang', 'general' and 'namespaces' keys. |
||
| 216 | * 'general' contains: 'wikiName', 'articlePath', 'scriptPath', 'script', |
||
| 217 | * 'timezone', and 'timezoneOffset'; 'namespaces' contains all namespace |
||
| 218 | * names, keyed by their IDs. If this function returns null, the API call |
||
| 219 | * failed. |
||
| 220 | */ |
||
| 221 | public function getMetadata(string $projectUrl): ?array |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get a list of projects that have opted in to having all their users' restricted statistics available to anyone. |
||
| 306 | * @return string[] |
||
| 307 | */ |
||
| 308 | public function optedIn(): array |
||
| 309 | { |
||
| 310 | $optedIn = $this->container->getParameter('opted_in'); |
||
| 311 | // In case there's just one given. |
||
| 312 | if (!is_array($optedIn)) { |
||
| 313 | $optedIn = [ $optedIn ]; |
||
| 314 | } |
||
| 315 | return $optedIn; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * The path to api.php. |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function getApiPath(): string |
||
| 323 | { |
||
| 324 | return $this->container->getParameter('api_path'); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get a page from the given Project. |
||
| 329 | * @param Project $project The project. |
||
| 330 | * @param string $pageName The name of the page. |
||
| 331 | * @return Page |
||
| 332 | */ |
||
| 333 | public function getPage(Project $project, string $pageName): Page |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Check to see if a page exists on this project and has some content. |
||
| 344 | * @param Project $project The project. |
||
| 345 | * @param int $namespaceId The page namespace ID. |
||
| 346 | * @param string $pageTitle The page title, without namespace. |
||
| 347 | * @return bool |
||
| 348 | */ |
||
| 349 | public function pageHasContent(Project $project, int $namespaceId, string $pageTitle): bool |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get a list of the extensions installed on the wiki. |
||
| 366 | * @param Project $project |
||
| 367 | * @return string[] |
||
| 368 | */ |
||
| 369 | public function getInstalledExtensions(Project $project): array |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get a list of users who are in one of the given user groups. |
||
| 388 | * @param Project $project |
||
| 389 | * @param string[] List of user groups to look for. |
||
|
|
|||
| 390 | * @return string[] with keys 'user_name' and 'ug_group' |
||
| 391 | */ |
||
| 392 | public function getUsersInGroups(Project $project, array $groups): array |
||
| 417 | } |
||
| 418 | } |
||
| 419 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths