Skip to content

Commit

Permalink
PhpStan level 9 support (full strict without mixed type).
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Nov 3, 2021
1 parent 26734c4 commit 498f021
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/CmsPluginPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public function getPanel(): string
$components = '';
$componentList = $this->pluginManager->getComponentsInfo();
usort($componentList, static function (array $a, array $b): int {
if (($a['position'] ?? 50) === ($b['position'] ?? 50)) {
if ($a['position'] === $b['position']) {
return $a['implements'] === $b['implements'] ? 1 : -1;
}

return ($a['position'] ?? 50) < ($b['position'] ?? 50) ? 1 : -1;
return $a['position'] < $b['position'] ? 1 : -1;
});
foreach ($componentList as $component) {
$matchPlugin = $pluginServiceType === $component['implements'];
Expand All @@ -126,7 +126,7 @@ public function getPanel(): string
? '<td style="background:#BDE678">' . htmlspecialchars($component['view']) . '</td>'
: '<td>' . htmlspecialchars($component['view']) . '</td>'
)
. '<td>' . htmlspecialchars((string) ($component['position'] ?? '???'), ENT_QUOTES) . '</td>'
. '<td>' . htmlspecialchars((string) $component['position'], ENT_QUOTES) . '</td>'
. '<td>' .
(function (array $params): string {
$return = '';
Expand All @@ -150,7 +150,7 @@ public function getPanel(): string
}

return $return;
})($component['params'] ?? [])
})($component['params'])
. '</td>'
. '</tr>';
}
Expand Down
32 changes: 18 additions & 14 deletions src/Component/VueComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ class VueComponent implements PluginComponent


/**
* @param mixed[] $config
* @param array{key: string, name: string, implements: class-string, componentClass: class-string, view: string, source: string, position?: int, tab?: string, params?: array<int|string, mixed>} $config
*/
public function __construct(array $config)
{
$this->key = (string) $config['key'];
$this->name = (string) $config['name'];
$this->tab = (string) ($config['tab'] ?? $config['key']);
$this->source = (string) $config['source'];
$this->position = (int) ($config['position'] ?? 50);
$this->key = $config['key'];
$this->name = $config['name'];
$this->tab = $config['tab'] ?? $config['key'];
$this->source = $config['source'];
$this->position = $config['position'] ?? 50;

$parameters = [];
foreach ($config['params'] ?? [] as $parameterName => $parameterValue) {
Expand All @@ -42,16 +42,20 @@ public function __construct(array $config)
$parameterValue = null;
}
if (\is_string($parameterName) === false) {
throw new \InvalidArgumentException(
'Component "' . $this->key . '": Parameter "' . $parameterName . '" must be '
. 'a string, but "' . get_debug_type($parameterName) . '" given.',
);
throw new \InvalidArgumentException(sprintf(
'Component "%s": Parameter "%s" must be a string, but "%s" given.',
$this->key,
$parameterName,
get_debug_type($parameterName),
));
}
if ($parameterValue !== null && is_scalar($parameterValue) === false) {
throw new \InvalidArgumentException(
'Component "' . $this->key . '": Parameter "' . $parameterName . '" value '
. 'must be scalar, but type "' . get_debug_type($parameterValue) . '" given.',
);
throw new \InvalidArgumentException(sprintf(
'Component "%s": Parameter "%s" value must be scalar, but type "%s" given.',
$this->key,
$parameterName,
get_debug_type($parameterValue),
));
}
$parameters[$parameterName] = $parameterValue;
}
Expand Down
29 changes: 21 additions & 8 deletions src/PluginComponentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public function beforeCompile(): void
return;
}

/** @var mixed[] $config */
/** @var array<string|int, array<string, mixed>> $config */
$config = $this->getConfig();

$components = [];
foreach ($config as $key => $component) {
if (\is_string($key) === false) {
throw new \RuntimeException('Component name must be string, but "' . $key . '" given.');
throw new \RuntimeException('Component name must be string, but "' . $key . '" (' . get_debug_type($key) . ') given.');
}
if (isset($component['name'], $component['implements'], $component['view'], $component['source']) === false) {
throw new \RuntimeException(
Expand Down Expand Up @@ -112,8 +112,13 @@ public function beforeCompile(): void
}
} catch (\ReflectionException $e) {
throw new \RuntimeException(
'Component "' . $key . '": Component class "' . $componentClass . '" is broken: ' . $e->getMessage(),
$e->getCode(),
sprintf(
'Component "%s": Component class "%s" is broken: %s',
$key,
$componentClass,
$e->getMessage(),
),
500,
$e,
);
}
Expand All @@ -138,8 +143,10 @@ public function beforeCompile(): void
. 'path "' . $source . '" given.',
);
}
$componentParameters = $component['params'] ?? [];
assert(is_array($componentParameters));
$params = [];
foreach ($component['params'] ?? [] as $parameter) {
foreach ($componentParameters as $parameter) {
if (is_array($parameter) && count($parameter) === 1) {
$parameterName = array_keys($parameter)[0] ?? throw new \RuntimeException('Broken parameter.');
$parameterValue = array_values($parameter)[0] ?? null;
Expand Down Expand Up @@ -170,15 +177,21 @@ public function beforeCompile(): void
}
$params[Strings::firstLower((string) $parameterName)] = $parameterValue;
}
$tab = $component['tab'] ?? $key;
$position = $component['position'] ?? 1;
$componentClass = $component['componentClass'] ?? VueComponent::class;
assert(is_string($tab));
assert(is_int($position));
assert(is_string($componentClass));
$components[] = (new ComponentDIDefinition(
key: $key,
name: trim(trim($name) === '' ? $key : $name),
implements: $implements,
componentClass: $component['componentClass'] ?? VueComponent::class,
componentClass: $componentClass,
view: $view,
source: $source,
position: (int) ($component['position'] ?? 1),
tab: (string) ($component['tab'] ?? $key),
position: $position,
tab: $tab,
params: $params,
))->toArray();
}
Expand Down
30 changes: 18 additions & 12 deletions src/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

final class PluginManager
{
/** @var array<int, mixed> */
/** @var array<int, array{key: string, name: string, implements: class-string, componentClass: class-string, view: string, source: string, position: int, tab: string, params: array<int|string, string|int|float|bool|null>}> */
private array $components = [];

/** @var array<int, mixed>|null */
/** @var array<class-string<Plugin>, array{service: string, type: class-string<Plugin>, name: string, realName: string, baseEntity: string|null, label: string, basePath: string, priority: int, icon: string|null, roles: array<int, string>, privileges: array<int, string>, menuItem: array<string, string|null>|null}>|null */
private ?array $pluginInfo = null;

/** @var string[] */
/** @var array<string, string> */
private array $baseEntityToPlugin;

/** @var string[] */
Expand All @@ -43,27 +43,29 @@ public function __construct(
/**
* Effective all Plugin services setter.
*
* @param string[] $pluginServices
* @param array<int, string> $pluginServices
* @internal use in DIC
*/
public function setPluginServices(array $pluginServices): void
{
/** @var array{hash: string, plugins: array<class-string<Plugin>, array{service: string, type: class-string<Plugin>, name: string, realName: string, baseEntity: string|null, label: string, basePath: string, priority: int, icon: string|null, roles: array<int, string>, privileges: array<int, string>, menuItem: array<string, string|null>|null}>, baseEntityToPlugin: array<class-string, class-string<Plugin>>, baseEntitySimpleToPlugin: array<string, class-string<Plugin>>, nameToType: array<string, class-string>}|null $cache */
$cache = $this->cache->load('plugin-info');
if ($cache === null || ((string) ($cache['hash'] ?? '')) !== $this->getPluginServicesHash($pluginServices)) {
if ($cache === null || $cache['hash'] !== $this->getPluginServicesHash($pluginServices)) {
[$plugins, $baseEntityToPlugin, $baseEntitySimpleToPlugin] = $this->processPluginInfo($pluginServices);

$pluginNameToType = [];
foreach ($plugins as $plugin) {
$pluginNameToType[$plugin['name']] = $plugin['type'];
}

$this->cache->save('plugin-info', $cache = [
$cache = [
'hash' => $this->getPluginServicesHash($pluginServices),
'plugins' => $plugins,
'baseEntityToPlugin' => $baseEntityToPlugin,
'baseEntitySimpleToPlugin' => $baseEntitySimpleToPlugin,
'nameToType' => $pluginNameToType,
]);
];
$this->cache->save('plugin-info', $cache);
}

$this->pluginInfo = $cache['plugins'];
Expand Down Expand Up @@ -95,7 +97,11 @@ public function getComponents(Plugin|PluginInfoEntity|string $plugin, ?string $v
try {
$baseEntityExist = class_exists($baseEntity);
} catch (\Throwable $e) {
throw new \RuntimeException('Base entity "' . $baseEntity . '" is broken: ' . $e->getMessage(), $e->getCode(), $e);
throw new \RuntimeException(
sprintf('Base entity "%s" is broken: %s', $baseEntity, $e->getMessage()),
500,
$e,
);
}
if ($baseEntityExist === false) {
throw new \InvalidArgumentException('Entity class "' . $baseEntity . '" does not exist or is not autoloadable.');
Expand Down Expand Up @@ -137,7 +143,7 @@ public function getComponents(Plugin|PluginInfoEntity|string $plugin, ?string $v


/**
* @param mixed[] $components
* @param array<int, array{key: string, name: string, implements: class-string, componentClass: class-string, view: string, source: string, position: int, tab: string, params: array<int|string, string|int|float|bool|null>}> $components
* @internal use in DIC
*/
public function addComponents(array $components): void
Expand All @@ -147,7 +153,7 @@ public function addComponents(array $components): void


/**
* @param mixed[] $component
* @param array{key: string, name: string, implements: class-string, componentClass: class-string, view: string, source: string, position: int, tab: string, params: array<int|string, string|int|float|bool|null>} $component
* @internal use in DIC
*/
public function addComponent(array $component): void
Expand All @@ -157,7 +163,7 @@ public function addComponent(array $component): void


/**
* @return mixed[]
* @return array<int, array{key: string, name: string, implements: class-string, componentClass: class-string, view: string, source: string, position: int, tab: string, params: array<int|string, string|int|float|bool|null>}>
*/
public function getComponentsInfo(): array
{
Expand Down Expand Up @@ -227,7 +233,7 @@ public function getPluginByType(string $type): Plugin


/**
* @return mixed[]
* @return array<class-string<Plugin>, array{service: string, type: class-string<Plugin>, name: string, realName: string, baseEntity: string|null, label: string, basePath: string, priority: int, icon: string|null, roles: array<int, string>, privileges: array<int, string>, menuItem: array<string, string|null>|null}>
*/
public function getPluginInfo(): array
{
Expand Down

0 comments on commit 498f021

Please sign in to comment.