Skip to content

Commit 154d15e

Browse files
authored
fix: phpstan level 5 errors #84 (#100)
1 parent 5330e17 commit 154d15e

File tree

16 files changed

+44
-147
lines changed

16 files changed

+44
-147
lines changed

.github/workflows/phpstan.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ jobs:
5656
key: ${{ runner.os }}-composer-2.4.8-${{ hashFiles('**/composer.json') }}
5757
restore-keys: ${{ runner.os }}-composer-2.4.8
5858

59-
- name: Clone Magento
59+
- name: Download Magento
6060
run: |
61-
git clone --depth=1 --branch=2.4.8 https://github.com/magento/magento2.git magento2
61+
composer create-project \
62+
--repository-url=https://mirror.mage-os.org/ \
63+
magento/project-community-edition \
64+
magento2
6265
6366
- name: Install Magento
6467
working-directory: magento2
@@ -109,11 +112,9 @@ jobs:
109112
composer update --with-dependencies
110113
111114
# Enable module
112-
bin/magento module:enable OpenForgeProject_MageForge
113115
bin/magento setup:upgrade
114116
115117
- name: Run PHPStan
116118
working-directory: magento2
117-
continue-on-error: true
118119
run: |
119120
vendor/bin/phpstan analyse -c vendor/openforgeproject/mageforge/phpstan.neon vendor/openforgeproject/mageforge/src

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
parameters:
2-
level: 6
2+
level: 5
33
paths:
44
- src

src/Block/Inspector.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,13 @@ class Inspector extends Template
1919
{
2020
private const XML_PATH_INSPECTOR_ENABLED = 'dev/mageforge_inspector/enabled';
2121

22-
private State $state;
23-
24-
private ScopeConfigInterface $scopeConfig;
25-
26-
private DevHelper $devHelper;
27-
28-
/**
29-
* @param Context $context
30-
* @param State $state
31-
* @param ScopeConfigInterface $scopeConfig
32-
* @param DevHelper $devHelper
33-
* @param array $data
34-
*/
3522
public function __construct(
3623
Context $context,
37-
State $state,
38-
ScopeConfigInterface $scopeConfig,
39-
DevHelper $devHelper,
24+
private readonly State $state,
25+
private readonly ScopeConfigInterface $scopeConfig,
26+
private readonly DevHelper $devHelper,
4027
array $data = []
4128
) {
42-
$this->state = $state;
43-
$this->scopeConfig = $scopeConfig;
44-
$this->devHelper = $devHelper;
4529
parent::__construct($context, $data);
4630
}
4731

src/Console/Command/Dev/InspectorCommand.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,12 @@ class InspectorCommand extends AbstractCommand
2121
private const XML_PATH_INSPECTOR_ENABLED = 'dev/mageforge_inspector/enabled';
2222
private const ARGUMENT_ACTION = 'action';
2323

24-
private WriterInterface $configWriter;
25-
26-
private State $state;
27-
28-
private CacheManager $cacheManager;
29-
30-
/**
31-
* @param WriterInterface $configWriter
32-
* @param State $state
33-
* @param CacheManager $cacheManager
34-
* @param string|null $name
35-
*/
3624
public function __construct(
37-
WriterInterface $configWriter,
38-
State $state,
39-
CacheManager $cacheManager,
25+
private readonly WriterInterface $configWriter,
26+
private readonly State $state,
27+
private readonly CacheManager $cacheManager,
4028
?string $name = null
4129
) {
42-
$this->configWriter = $configWriter;
43-
$this->state = $state;
44-
$this->cacheManager = $cacheManager;
4530
parent::__construct($name);
4631
}
4732

@@ -116,7 +101,6 @@ protected function executeCommand(InputInterface $input, OutputInterface $output
116101
'enable' => $this->enableInspector(),
117102
'disable' => $this->disableInspector(),
118103
'status' => $this->showStatus(),
119-
default => Cli::RETURN_FAILURE,
120104
};
121105
}
122106

src/Console/Command/System/CheckCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private function getMysqlVersionViaPdo(): ?string
238238
/**
239239
* Get database configuration from environment variables
240240
*
241-
* @return array
241+
* @return array<string, string>
242242
*/
243243
private function getDatabaseConfig(): array
244244
{
@@ -500,7 +500,7 @@ private function checkSearchEngineConnections(): ?string
500500
/**
501501
* Get potential search engine hosts
502502
*
503-
* @return array
503+
* @return string[]
504504
*/
505505
private function getSearchEngineHosts(): array
506506
{
@@ -531,7 +531,7 @@ private function getSearchEngineHosts(): array
531531
/**
532532
* Format search engine version output
533533
*
534-
* @param array $info
534+
* @param array<string, mixed> $info
535535
* @return string
536536
*/
537537
private function formatSearchEngineVersion(array $info): string
@@ -549,7 +549,7 @@ private function formatSearchEngineVersion(array $info): string
549549
* Test Elasticsearch connection and return version info
550550
*
551551
* @param string $url
552-
* @return array|bool
552+
* @return array<string, mixed>|false
553553
*/
554554
private function testElasticsearchConnection(string $url)
555555
{
@@ -573,7 +573,7 @@ private function testElasticsearchConnection(string $url)
573573
* Try to connect using Magento's HTTP client
574574
*
575575
* @param string $url
576-
* @return array|null
576+
* @return array<string, mixed>|null
577577
*/
578578
private function tryMagentoHttpClient(string $url): ?array
579579
{
@@ -603,7 +603,7 @@ private function tryMagentoHttpClient(string $url): ?array
603603
/**
604604
* Get important PHP extensions
605605
*
606-
* @return array
606+
* @return array<int, array<int, string>>
607607
*/
608608
private function getImportantPhpExtensions(): array
609609
{
@@ -757,8 +757,8 @@ private function getSystemEnvironmentValue(string $name): ?string
757757
{
758758
// Use ini_get for certain system variables as a safer alternative
759759
if (in_array($name, ['memory_limit', 'max_execution_time'])) {
760-
$value = ini_get($name);
761-
if ($value !== false) {
760+
$value = (string)ini_get($name);
761+
if ($value !== '') {
762762
return $value;
763763
}
764764
}

src/Console/Command/System/VersionCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
class VersionCommand extends AbstractCommand
1818
{
1919
private const API_URL = 'https://api.github.com/repos/openforgeproject/mageforge/releases/latest';
20-
private const PACKAGE_NAME = 'openforgeproject/mageforge';
2120
private const UNKNOWN_VERSION = 'Unknown';
2221

2322
/**

src/Console/Command/Theme/BuildCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ private function getServerVar(string $name): ?string
492492
private function setEnvVar(string $name, string $value): void
493493
{
494494
// Validate input parameters
495-
if (empty($name) || !is_string($name)) {
495+
if (empty($name) ) {
496496
return;
497497
}
498498

src/Console/Command/Theme/CleanCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ private function getServerVar(string $name): ?string
638638
*/
639639
private function setEnvVar(string $name, string $value): void
640640
{
641-
if (empty($name) || !is_string($name)) {
641+
if (empty($name) ) {
642642
return;
643643
}
644644

src/Console/Command/Theme/TokensCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private function validateHyvaTheme(string $themeCode, OutputInterface $output):
125125

126126
// If no theme was selected, exit
127127
if ($correctedTheme === null) {
128-
return Cli::RETURN_FAILURE;
128+
return null;
129129
}
130130

131131
// Use the corrected theme code
@@ -135,7 +135,7 @@ private function validateHyvaTheme(string $themeCode, OutputInterface $output):
135135
// Double-check the corrected theme exists
136136
if ($themePath === null) {
137137
$this->io->error("Theme $themeCode is not installed.");
138-
return Cli::RETURN_FAILURE;
138+
return null;
139139
}
140140

141141
$this->io->info("Using theme: $themeCode");

src/Model/TemplateEngine/Decorator/InspectorHints.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace OpenForgeProject\MageForge\Model\TemplateEngine\Decorator;
66

7-
use Magento\Framework\App\ObjectManager;
87
use Magento\Framework\Math\Random;
98
use Magento\Framework\View\Element\AbstractBlock;
109
use Magento\Framework\View\Element\BlockInterface;
@@ -17,28 +16,18 @@
1716
*/
1817
class InspectorHints implements TemplateEngineInterface
1918
{
20-
private TemplateEngineInterface $subject;
21-
22-
private bool $showBlockHints;
23-
24-
private ?Random $random = null;
25-
2619
private string $magentoRoot;
2720

2821
/**
2922
* @param TemplateEngineInterface $subject
3023
* @param bool $showBlockHints
31-
* @param Random|null $random
24+
* @param Random $random
3225
*/
3326
public function __construct(
34-
TemplateEngineInterface $subject,
35-
bool $showBlockHints,
36-
?Random $random = null
27+
private readonly TemplateEngineInterface $subject,
28+
private readonly bool $showBlockHints,
29+
private readonly Random $random
3730
) {
38-
$this->subject = $subject;
39-
$this->showBlockHints = $showBlockHints;
40-
$this->random = $random ?? ObjectManager::getInstance()->get(Random::class);
41-
4231
// Get Magento root directory - try multiple strategies
4332
// 1. Try from BP constant (most reliable)
4433
if (defined('BP')) {
@@ -62,6 +51,10 @@ public function render(BlockInterface $block, $templateFile, array $dictionary =
6251
{
6352
$result = $this->subject->render($block, $templateFile, $dictionary);
6453

54+
if (!$this->showBlockHints) {
55+
return $result;
56+
}
57+
6558
// Only inject attributes if there's actual HTML content
6659
if (empty(trim($result))) {
6760
return $result;
@@ -189,7 +182,7 @@ private function getParentBlockName(BlockInterface $block): string
189182
{
190183
if ($block instanceof AbstractBlock) {
191184
$parent = $block->getParentBlock();
192-
if ($parent && method_exists($parent, 'getNameInLayout')) {
185+
if ($parent instanceof AbstractBlock) {
193186
return $parent->getNameInLayout() ?: '';
194187
}
195188
}
@@ -205,7 +198,7 @@ private function getParentBlockName(BlockInterface $block): string
205198
*/
206199
private function getBlockAlias(BlockInterface $block): string
207200
{
208-
if ($block instanceof AbstractBlock && method_exists($block, 'getNameInLayout')) {
201+
if ($block instanceof AbstractBlock) {
209202
return $block->getNameInLayout() ?: '';
210203
}
211204

0 commit comments

Comments
 (0)