src/Plugin/PluginTrait.php
<?php namespace Nopolabs\Yabot\Plugin; Possibly zero references to use statement for classlike/namespace `Exception` `(\Exception)`use Exception;use Nopolabs\Yabot\Helpers\ConfigTrait;use Nopolabs\Yabot\Helpers\LogTrait;use Nopolabs\Yabot\Message\Message;use Throwable; trait PluginTrait{ use LogTrait; use ConfigTrait; private $pluginId; /** @var PluginMatcher */ private $pluginMatcher; /** @var MethodMatcher[] */ private $methodMatchers; public function help(): string { return $this->get('help', 'no help available'); } public function status(): string { return 'running'; } public function init(string $pluginId, array $params) { $this->pluginId = $pluginId; $this->overrideConfig($params); $this->methodMatchers = $this->buildMethodMatchers($this->get('matchers'));Line exceeds 120 characters; contains 137 characters $this->pluginMatcher = new PluginMatcher($pluginId, $this->getIsBot(), $this->getChannels(), $this->getUsers(), $this->getLog()); $this->info("inited $pluginId config:", $this->getConfig()); } public function replaceInPatterns($search, $replace, array $matchers): array { $replaced = []; foreach ($matchers as $name => $params) {Expected 1 space after FUNCTION keyword; 0 found $params['patterns'] = array_map(function($pattern) use ($search, $replace) { return str_replace($search, $replace, $pattern); }, $params['patterns']); $replaced[$name] = $params; } return $replaced; } public function getPriority(): int { return $this->get('priority'); } public function getPrefix(): string { return $this->get('prefix'); } public function handle(Message $message) { if (empty($this->pluginMatch($message))) { return; } $this->methodMatch($message); } protected function pluginMatch(Message $message): array { return $this->getPluginMatcher()->matches($message); } protected function getPluginMatcher() : PluginMatcher { return $this->pluginMatcher; } Function `methodMatch` has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Avoid assigning values to variables in if clauses and the like (line '89', column '17'). protected function methodMatch(Message $message) { foreach ($this->getMethodMatchers() as $methodMatcher) { /** @var MethodMatcher $methodMatcher */ if ($matches = $methodMatcher->matches($message)) { $this->dispatchMatch($methodMatcher, $message, $matches); if ($message->isHandled()) { return; } } } } protected function dispatchMatch(MethodMatcher $methodMatcher, Message $message, array $matches) { $method = $methodMatcher->getMethod(); $this->info("dispatching {$this->pluginId}:{$methodMatcher->getName()}:$method ".json_encode($matches)); $this->dispatch($method, $message, $matches); } protected function getMethodMatchers() : array { return $this->methodMatchers; } protected function dispatch(string $method, Message $message, array $matches) { if (!method_exists($this, $method)) { $this->warning("{$this->pluginId} no method named: $method"); return; } Blank line found at start of control structure try { $this->$method($message, $matches);Blank line found at end of control structure } catch (Throwable $throwable) { $errmsg = 'Exception in '.static::class.'::'.$method."\n" .$throwable->getMessage()."\n" .$throwable->getTraceAsString(); $this->warning($errmsg); } } protected function overrideConfig(array $params) { $config = $this->canonicalConfig(array_merge($this->getConfig(), $params)); $this->setConfig($config); } protected function setPluginId($pluginId) { $this->pluginId = $pluginId; } protected function getUsers() { return $this->get('users'); } protected function getChannels() { return $this->get('channels'); } /** * @return bool|null */ protected function getIsBot() { return $this->get('isBot'); } protected function getMatchers(): array { return $this->get('matchers'); } protected function canonicalConfig(array $config): array { $config['priority'] = $config['priority'] ?? PluginManager::DEFAULT_PRIORITY; $config['prefix'] = ($config['prefix'] ?? '') ? $config['prefix'] : PluginManager::NO_PREFIX; $config['isBot'] = $config['isBot'] ?? null; $config['channels'] = $config['channels'] ?? array_filter([$config['channel'] ?? null]); $config['users'] = $config['users'] ?? array_filter([$config['user'] ?? null]); $config['matchers'] = $this->canonicalMatchers($config['matchers'] ?? []); unset($config['channel']); unset($config['user']); return $config; } protected function canonicalMatchers(array $matchers): array { $expanded = []; foreach ($matchers as $name => $params) { $expanded[$name] = $this->canonicalMatcher($name, $params); } if (empty($expanded)) { $this->warning("{$this->pluginId} has no matchers"); } return $expanded; } protected function canonicalMatcher(string $name, $params) : array { $params = is_array($params) ? $params : ['patterns' => [$params]]; $method = $params['method'] ?? $name; if (!method_exists($this, $method)) { $this->warning("{$this->pluginId} no method named: $method"); } $params['isBot'] = $params['isBot'] ?? null; $params['channels'] = $params['channels'] ?? array_filter([$params['channel'] ?? null]); $params['users'] = $params['users'] ?? array_filter([$params['user'] ?? null]); $params['patterns'] = $params['patterns'] ?? array_filter([$params['pattern'] ?? null]); $params['method'] = $method; unset($params['channel']); unset($params['user']); unset($params['pattern']); return $params; } protected function buildMethodMatchers(array $matchers) : array { $methodMatchers = []; foreach ($matchers as $name => $params) { $methodMatchers[] = new MethodMatcher( $name, $params['isBot'], $params['channels'], $params['users'], $this->validPatterns($params['patterns'], $name), $params['method'], $this->getLog() ); } return $methodMatchers; } protected function validPatterns(array $patterns, $name) : array { $valid = []; foreach ($patterns as $pattern) { if ($this->isValidRegExp($pattern, $name)) { $valid[] = $pattern; } } return $valid; } protected function isValidRegExp($pattern, $name) : bool { try {Argument #1 of this call to `\preg_match` is typically a literal or constant but isn't, but argument #2 (which is typically a variable) is a literal or constant. The arguments may be in the wrong order. preg_match($pattern, ''); return true; } catch (Throwable $e) { $this->warning("$name.pattern='$pattern' ".$e->getMessage()); return false; } }Expected 1 newline at end of file; 0 found}