Skip to content

Commit f41408c

Browse files
Morgy93claude
andcommitted
fix: resolve mago analyze type-safety findings in module source
Fix the type errors mago analyze reports against a full Magento install: - CheckCommand: cast ini_get('memory_limit') (string|false declared string); use the PHP_VERSION constant instead of phpversion(); read the MySQL version via fetchOne('SELECT VERSION()') instead of from(null). - Type loose framework return values via inline @var (getPaths() -> array<string,string>, getAllBlocks() -> BlockInterface[]). - Cast mixed operands/arguments: theme loop index, hrtime() metrics, stat() mode, and the getOption() flags used in a boolean expression. - InspectorHints: widen render()'s $dictionary param to match the parent TemplateEngineInterface; cast the BP constant to string. - Drop redundant (string) casts and an obsolete @var docblock. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0d4aa50 commit f41408c

12 files changed

Lines changed: 26 additions & 34 deletions

File tree

‎src/Console/Command/Hyva/CompatibilityCheckCommand.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
9595
{
9696
// Check if we're in interactive mode (no options provided)
9797
$hasOptions =
98-
$input->getOption(self::OPTION_SHOW_ALL)
99-
|| $input->getOption(self::OPTION_THIRD_PARTY_ONLY)
100-
|| $input->getOption(self::OPTION_INCLUDE_VENDOR)
101-
|| $input->getOption(self::OPTION_DETAILED);
98+
(bool) $input->getOption(self::OPTION_SHOW_ALL)
99+
|| (bool) $input->getOption(self::OPTION_THIRD_PARTY_ONLY)
100+
|| (bool) $input->getOption(self::OPTION_INCLUDE_VENDOR)
101+
|| (bool) $input->getOption(self::OPTION_DETAILED);
102102

103103
if (!$hasOptions && $this->isInteractiveTerminal($output)) {
104104
return $this->runInteractiveMode($input, $output);

‎src/Console/Command/System/CheckCommand.php‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ protected function configure(): void
6262
*/
6363
protected function executeCommand(InputInterface $input, OutputInterface $output): int
6464
{
65-
$phpVersion = phpversion();
65+
$phpVersion = PHP_VERSION;
6666
$nodeVersion = $this->getNodeVersion();
6767
$mysqlVersion = $this->getShortMysqlVersion();
6868
$dbType = $this->getDatabaseType();
6969
$osInfo = $this->getShortOsInfo();
7070
$magentoVersion = $this->productMetadata->getVersion();
71-
/** @var string $latestLtsNodeVersion */
7271
$latestLtsNodeVersion = $this->escaper->escapeHtml($this->getLatestLtsNodeVersion());
7372
$composerVersion = $this->getComposerVersion();
7473
$npmVersion = $this->getNpmVersion();
@@ -212,8 +211,7 @@ private function getMysqlVersionViaMagento(): ?string
212211
{
213212
try {
214213
$connection = $this->resourceConnection->getConnection();
215-
$select = $connection->select()->from(null, new \Zend_Db_Expr('VERSION()'));
216-
$version = $connection->fetchOne($select);
214+
$version = $connection->fetchOne('SELECT VERSION()');
217215

218216
return !empty($version) ? $version : null;
219217
} catch (\Exception $e) {
@@ -661,7 +659,7 @@ private function getImportantPhpExtensions(): array
661659
*/
662660
private function getPhpMemoryLimit(): string
663661
{
664-
return ini_get('memory_limit');
662+
return (string) ini_get('memory_limit');
665663
}
666664

667665
/**

‎src/Console/Command/Theme/BuildCommand.php‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
9898
label: 'Select themes to build',
9999
options: static fn(string $value) => empty($value)
100100
? $options
101-
: array_values(array_filter(
102-
$options,
103-
static fn($option) => stripos((string) $option, $value) !== false,
104-
)),
101+
: array_values(array_filter($options, static fn($option) => stripos($option, $value) !== false)),
105102
placeholder: 'Type to search theme...',
106103
hint: 'Type to search, arrow keys to navigate, Space to toggle, Enter to confirm',
107104
required: false,
@@ -185,7 +182,7 @@ private function processBuildThemes(
185182
} else {
186183
// Use the existing spinner with a customized message
187184
foreach ($themeCodes as $index => $themeCode) {
188-
$currentTheme = $index + 1;
185+
$currentTheme = (int) $index + 1;
189186

190187
// Validate theme and handle suggestions BEFORE showing spinner
191188
$validatedTheme = $this->validateAndCorrectTheme($themeCode, $io, $output);

‎src/Console/Command/Theme/CleanCommand.php‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,7 @@ private function promptForThemes(array $options, array $themes): ?array
201201
label: 'Select themes to clean',
202202
options: static fn(string $value) => empty($value)
203203
? $options
204-
: array_values(array_filter(
205-
$options,
206-
static fn($option) => stripos((string) $option, $value) !== false,
207-
)),
204+
: array_values(array_filter($options, static fn($option) => stripos($option, $value) !== false)),
208205
placeholder: 'Type to search theme...',
209206
hint: 'Type to search, arrow keys to navigate, Space to toggle, Enter to confirm',
210207
required: false,
@@ -245,7 +242,7 @@ private function processThemes(array $themeCodes, bool $dryRun, OutputInterface
245242
$failedThemes = [];
246243

247244
foreach ($themeCodes as $index => $themeName) {
248-
$currentTheme = $index + 1;
245+
$currentTheme = (int) $index + 1;
249246

250247
// Validate and potentially correct theme name
251248
$validatedTheme = $this->validateTheme($themeName, $failedThemes, $output);

‎src/Console/Command/Theme/TokensCommand.php‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ private function selectTheme(?string $themeCode): ?string
113113
label: 'Select theme to generate tokens for',
114114
options: static fn(string $value) => empty($value)
115115
? $options
116-
: array_values(array_filter(
117-
$options,
118-
static fn($option) => stripos((string) $option, $value) !== false,
119-
)),
116+
: array_values(array_filter($options, static fn($option) => stripos($option, $value) !== false)),
120117
placeholder: 'Type to search theme...',
121118
scroll: 10,
122119
hint: 'Type to search, arrow keys to navigate, Enter to confirm',

‎src/Console/Command/Theme/WatchCommand.php‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,7 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
7777
label: 'Select theme to watch',
7878
options: static fn(string $value) => empty($value)
7979
? $options
80-
: array_values(array_filter(
81-
$options,
82-
static fn($option) => stripos((string) $option, $value) !== false,
83-
)),
80+
: array_values(array_filter($options, static fn($option) => stripos($option, $value) !== false)),
8481
placeholder: 'Type to search theme...',
8582
scroll: 10,
8683
hint: 'Type to search, arrow keys to navigate, Enter to confirm',

‎src/Model/TemplateEngine/Decorator/InspectorHints.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(
5353
private function resolveMagentoRoot(): string
5454
{
5555
if (defined('BP')) {
56-
return BP;
56+
return (string) BP;
5757
}
5858

5959
// vendor/openforgeproject/mageforge/src/Model/TemplateEngine/Decorator/InspectorHints.php
@@ -106,14 +106,14 @@ private function isExcludedTemplate(string $templateFile): bool
106106
* @param BlockInterface $block
107107
* @param string $templateFile
108108
* @param array $dictionary
109-
* @phpstan-param array<string, mixed> $dictionary
109+
* @phpstan-param array<array-key, mixed> $dictionary
110110
* @return string
111111
*/
112112
public function render(BlockInterface $block, $templateFile, array $dictionary = []): string
113113
{
114114
// Measure render time
115115
$startTime = hrtime(true);
116-
$result = (string) $this->subject->render($block, $templateFile, $dictionary);
116+
$result = $this->subject->render($block, $templateFile, $dictionary);
117117
$endTime = hrtime(true);
118118

119119
if (!$this->showBlockHints) {
@@ -141,8 +141,8 @@ public function render(BlockInterface $block, $templateFile, array $dictionary =
141141

142142
$renderMetrics = [
143143
'renderTimeMs' => round($renderTimeMs, 2),
144-
'startTime' => $startTime,
145-
'endTime' => $endTime,
144+
'startTime' => (int) $startTime,
145+
'endTime' => (int) $endTime,
146146
];
147147

148148
return $this->injectInspectorAttributes($result, $block, $templateFile, $renderMetrics);

‎src/Model/ThemePath.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function __construct(
2525
*/
2626
public function getPath(string $themeCode): ?string
2727
{
28+
/** @var array<string, string> $registeredThemes */
2829
$registeredThemes = $this->componentRegistrar->getPaths(ComponentRegistrar::THEME);
2930

3031
return $registeredThemes['frontend/' . $themeCode] ?? $registeredThemes['adminhtml/' . $themeCode] ?? null;

‎src/Service/Hyva/CompatibilityChecker.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function check(
6969
bool $thirdPartyOnly = false,
7070
bool $excludeVendor = true,
7171
): array {
72+
/** @var array<string, string> $modules */
7273
$modules = $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE);
7374
$results = [
7475
'modules' => [],

‎src/Service/Hyva/IncompatibilityDetector.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ public function detectInFile(string $filePath): array
128128
$content = $this->fileDriver->fileGetContents($filePath);
129129
$lines = explode("\n", $content);
130130

131-
return $this->scanContentForPatterns($lines, self::INCOMPATIBLE_PATTERNS[$fileType]);
131+
/** @var array<int, array{pattern: string, description: string, severity: string}> $patterns */
132+
$patterns = self::INCOMPATIBLE_PATTERNS[$fileType];
133+
134+
return $this->scanContentForPatterns($lines, $patterns);
132135
} catch (\Exception $e) {
133136
return [];
134137
}

0 commit comments

Comments
 (0)