| Total Complexity | 43 |
| Total Lines | 207 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PluginHelper 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 PluginHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class PluginHelper |
||
| 17 | { |
||
| 18 | /** @var array<string,string> normalized_title => OriginalTitle */ |
||
| 19 | private array $titleMap = []; |
||
| 20 | |||
| 21 | public function __construct( |
||
| 22 | private readonly ParameterBagInterface $parameterBag, |
||
| 23 | private readonly AccessUrlRelPluginRepository $pluginRelRepo, |
||
| 24 | private readonly PluginRepository $pluginRepo, |
||
| 25 | private readonly AccessUrlHelper $accessUrlHelper, |
||
| 26 | ) { |
||
| 27 | $this->titleMap = []; |
||
| 28 | } |
||
| 29 | |||
| 30 | private static function normalize(string $s): string |
||
| 31 | { |
||
| 32 | return strtolower(preg_replace('/[^a-z0-9]/i', '', $s)); |
||
| 33 | } |
||
| 34 | |||
| 35 | private function buildTitleMap(): void |
||
| 36 | { |
||
| 37 | if (!empty($this->titleMap)) { |
||
| 38 | return; |
||
| 39 | } |
||
| 40 | $all = $this->pluginRepo->findAll(); |
||
| 41 | foreach ($all as $p) { |
||
| 42 | /** @var PluginEntity $p */ |
||
| 43 | $title = $p->getTitle(); |
||
| 44 | $norm = self::normalize($title); |
||
| 45 | $this->titleMap[$norm] = $title; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | private function resolveTitle(string $name): ?string |
||
| 50 | { |
||
| 51 | $this->buildTitleMap(); |
||
| 52 | |||
| 53 | $norm = self::normalize($name); |
||
| 54 | if (isset($this->titleMap[$norm])) { |
||
| 55 | return $this->titleMap[$norm]; |
||
| 56 | } |
||
| 57 | |||
| 58 | $studly = implode('', array_map('ucfirst', preg_split('/[^a-z0-9]+/i', $name))); |
||
| 59 | $candidates = array_unique([ |
||
| 60 | $name, |
||
| 61 | ucfirst(strtolower($name)), |
||
| 62 | strtolower($name), |
||
| 63 | strtoupper($name), |
||
| 64 | $studly, |
||
| 65 | self::normalize($studly) |
||
| 66 | ]); |
||
| 67 | |||
| 68 | foreach ($candidates as $cand) { |
||
| 69 | $normCand = self::normalize((string) $cand); |
||
| 70 | if (isset($this->titleMap[$normCand])) { |
||
| 71 | return $this->titleMap[$normCand]; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return null; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function loadLegacyPlugin(string $pluginName): ?object |
||
| 79 | { |
||
| 80 | $projectDir = $this->parameterBag->get('kernel.project_dir'); |
||
| 81 | |||
| 82 | $cands = array_unique([ |
||
| 83 | $pluginName, |
||
| 84 | implode('', array_map('ucfirst', preg_split('/[^a-z0-9]+/i', $pluginName))), |
||
| 85 | ]); |
||
| 86 | |||
| 87 | foreach ($cands as $cand) { |
||
| 88 | $pluginPath = $projectDir.'/public/plugin/'.$cand .'/src/'.$cand .'.php'; |
||
| 89 | $pluginClass = $cand; |
||
| 90 | |||
| 91 | if (!file_exists($pluginPath)) { |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | if (!class_exists($pluginClass)) { |
||
| 95 | require_once $pluginPath; |
||
| 96 | } |
||
| 97 | if (class_exists($pluginClass) && method_exists($pluginClass, 'create')) { |
||
| 98 | return $pluginClass::create(); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return null; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getPluginSetting(string $pluginName, string $settingKey): mixed |
||
| 114 | } |
||
| 115 | |||
| 116 | public function isPluginEnabled(string $pluginName): bool |
||
| 117 | { |
||
| 118 | $accessUrl = $this->accessUrlHelper->getCurrent(); |
||
| 119 | if (!$accessUrl instanceof AccessUrl) { |
||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | $realTitle = $this->resolveTitle($pluginName); |
||
| 124 | if (null === $realTitle) { |
||
| 125 | return false; |
||
| 126 | } |
||
| 127 | |||
| 128 | $plugin = $this->pluginRepo->findOneBy(['title' => $realTitle]); |
||
| 129 | if (!$plugin || !$plugin->isInstalled()) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | $rel = $this->pluginRelRepo->findOneByPluginName($realTitle, $accessUrl->getId()); |
||
| 134 | return $rel && $rel->isActive(); |
||
| 135 | } |
||
| 136 | |||
| 137 | public function shouldBlockAccessByPositioning(?int $userId, int $courseId, ?int $sessionId): bool |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Return the whole configuration array for a plugin in the current Access URL, |
||
| 167 | * or null if not found. |
||
| 168 | */ |
||
| 169 | public function getPluginConfiguration(string $pluginName): ?array |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get a single configuration value from the plugin configuration JSON. |
||
| 192 | * Tries both the plain key ($key) and the legacy-prefixed key ($pluginName.'_'.$key). |
||
| 193 | * Falls back to legacy plugin::get($key) if available. |
||
| 194 | */ |
||
| 195 | public function getPluginConfigValue(string $pluginName, string $key, mixed $default = null): mixed |
||
| 225 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: