Skip to content

Commit cb90564

Browse files
authored
fix: resolve phpcs errors (#145)
* fix: remove continue-on-error from PHPCS step * fix: streamline token generation command execution * fix: simplify method return type formatting * fix: update parameter type hints for display methods * fix: add verbose warning for version fetch failure * fix: improve error handling and verbosity in CheckCommand * fix: update parameter type hint for constructor data * fix: add verbose warning for outdated package check failure * fix: update constructor and resolveMagentoRoot method * fix: update parameter types and improve phpstan annotations * fix: replace pathinfo with custom extension method * fix: replace basename and pathinfo with custom methods * fix: update cache handling and parameter types * fix: refactor build and watch commands for better execution * fix: improve build and watch command execution * fix: streamline npm command execution in NodePackageManager * fix: remove shell argument sanitization for theme code * fix: replace direct symlink check with method call * fix: normalize path separators in getExtensionFromPath * fix: normalize path separators in getBasename method * fix: simplify symlink check in SymlinkCleaner * fix: enable TTY support for npm watch process * fix: improve symlink check using file stat * fix: enable TTY support for npm watch process * fix: handle TTY support gracefully in process
1 parent 81e5e28 commit cb90564

17 files changed

Lines changed: 269 additions & 224 deletions

File tree

.github/workflows/phpcs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ jobs:
2525
run: composer create-project magento/magento-coding-standard --stability=dev /tmp/magento-coding-standard
2626

2727
- name: Run PHPCS
28-
continue-on-error: true
2928
run: /tmp/magento-coding-standard/vendor/bin/phpcs -p -s --standard=Magento2 src/

src/Block/Inspector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class Inspector extends Template
2424
* @param State $state
2525
* @param ScopeConfigInterface $scopeConfig
2626
* @param DevHelper $devHelper
27-
* @param array<string, mixed> $data
27+
* @param array $data
28+
* @phpstan-param array<string, mixed> $data
2829
*/
2930
public function __construct(
3031
Context $context,

src/Console/Command/Hyva/CompatibilityCheckCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ private function runScan(
274274
/**
275275
* Display compatibility check results
276276
*
277-
* @param array<string, mixed> $results
277+
* @param array $results
278+
* @phpstan-param array<string, mixed> $results
278279
* @param bool $showAll
279280
*/
280281
private function displayResults(array $results, bool $showAll): void
@@ -297,7 +298,8 @@ private function displayResults(array $results, bool $showAll): void
297298
/**
298299
* Display detailed file-level issues
299300
*
300-
* @param array<string, mixed> $results
301+
* @param array $results
302+
* @phpstan-param array<string, mixed> $results
301303
*/
302304
private function displayDetailedIssues(array $results): void
303305
{
@@ -337,7 +339,8 @@ private function displayDetailedIssues(array $results): void
337339
/**
338340
* Display summary statistics
339341
*
340-
* @param array<string, mixed> $results
342+
* @param array $results
343+
* @phpstan-param array<string, mixed> $results
341344
*/
342345
private function displaySummary(array $results): void
343346
{

src/Console/Command/System/CheckCommand.php

Lines changed: 50 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,16 @@ private function getNodeVersion(): string
146146
private function getLatestLtsNodeVersion(): string
147147
{
148148
try {
149-
$nodeData = file_get_contents(self::NODE_LTS_URL);
150-
if ($nodeData === false) {
149+
$httpClient = $this->httpClientFactory->create();
150+
$httpClient->setTimeout(2);
151+
$httpClient->get(self::NODE_LTS_URL);
152+
153+
if ($httpClient->getStatus() !== 200) {
154+
return 'Unknown';
155+
}
156+
157+
$nodeData = $httpClient->getBody();
158+
if ($nodeData === '') {
151159
return 'Unknown';
152160
}
153161

@@ -163,6 +171,9 @@ private function getLatestLtsNodeVersion(): string
163171
}
164172
return 'Unknown';
165173
} catch (\Exception $e) {
174+
if ($this->io->isVerbose()) {
175+
$this->io->warning('Failed to fetch latest Node.js LTS version: ' . $e->getMessage());
176+
}
166177
return 'Unknown';
167178
}
168179
}
@@ -185,11 +196,6 @@ private function getShortMysqlVersion(): string
185196
return $version;
186197
}
187198

188-
$version = $this->getMysqlVersionViaPdo();
189-
if (!empty($version)) {
190-
return $version;
191-
}
192-
193199
return 'Unknown';
194200
}
195201

@@ -202,10 +208,14 @@ private function getMysqlVersionViaMagento(): ?string
202208
{
203209
try {
204210
$connection = $this->resourceConnection->getConnection();
205-
$version = $connection->fetchOne('SELECT VERSION()');
211+
$select = $connection->select()->from(null, new \Zend_Db_Expr('VERSION()'));
212+
$version = $connection->fetchOne($select);
206213

207214
return !empty($version) ? $version : null;
208215
} catch (\Exception $e) {
216+
if ($this->io->isVerbose()) {
217+
$this->io->warning('Failed to read MySQL version via Magento connection: ' . $e->getMessage());
218+
}
209219
return null;
210220
}
211221
}
@@ -220,6 +230,9 @@ private function getMysqlVersionViaClient(): ?string
220230
try {
221231
$output = trim($this->shell->execute('mysql --version 2>/dev/null'));
222232
} catch (\Exception $e) {
233+
if ($this->io->isVerbose()) {
234+
$this->io->warning('Failed to read MySQL version via client: ' . $e->getMessage());
235+
}
223236
return null;
224237
}
225238

@@ -231,66 +244,6 @@ private function getMysqlVersionViaClient(): ?string
231244
return null;
232245
}
233246

234-
/**
235-
* Get MySQL version via PDO connection
236-
*
237-
* @return string|null
238-
*/
239-
private function getMysqlVersionViaPdo(): ?string
240-
{
241-
try {
242-
$config = $this->getDatabaseConfig();
243-
244-
// Default values if nothing is found
245-
$host = $config['host'] ?? 'localhost';
246-
$port = $config['port'] ?? '3306';
247-
$user = $config['user'] ?? 'root';
248-
$pass = $config['pass'] ?? '';
249-
250-
$dsn = "mysql:host=$host;port=$port";
251-
$pdo = new \PDO($dsn, $user, $pass, [\PDO::ATTR_TIMEOUT => 1]);
252-
$stmt = $pdo->query('SELECT VERSION()');
253-
if ($stmt === false) {
254-
return null;
255-
}
256-
257-
$version = $stmt->fetchColumn();
258-
259-
return !empty($version) ? (string)$version : null;
260-
} catch (\Exception $e) {
261-
return null;
262-
}
263-
}
264-
265-
/**
266-
* Get database configuration from environment variables
267-
*
268-
* @return array<string, string>
269-
*/
270-
private function getDatabaseConfig(): array
271-
{
272-
$envMapping = [
273-
'host' => ['DB_HOST', 'MYSQL_HOST', 'MAGENTO_DB_HOST'],
274-
'port' => ['DB_PORT', 'MYSQL_PORT', 'MAGENTO_DB_PORT', '3306'],
275-
'user' => ['DB_USER', 'MYSQL_USER', 'MAGENTO_DB_USER'],
276-
'pass' => ['DB_PASSWORD', 'MYSQL_PASSWORD', 'MAGENTO_DB_PASSWORD'],
277-
'name' => ['DB_NAME', 'MYSQL_DATABASE', 'MAGENTO_DB_NAME'],
278-
];
279-
280-
$config = [];
281-
foreach ($envMapping as $key => $envVars) {
282-
foreach ($envVars as $env) {
283-
$value = $this->getEnvironmentVariable($env);
284-
if ($value !== null) {
285-
$config[$key] = $value;
286-
break;
287-
}
288-
}
289-
}
290-
291-
return $config;
292-
}
293-
294247
/**
295248
* Get database type
296249
*
@@ -439,7 +392,9 @@ private function getSearchEngineFromMagentoConfig(): ?string
439392
return $resolverResult;
440393
}
441394
} catch (\Exception $e) {
442-
// Ignore general exceptions
395+
if ($this->io->isVerbose()) {
396+
$this->io->warning('Failed to read search engine config from Magento: ' . $e->getMessage());
397+
}
443398
}
444399

445400
return null;
@@ -469,7 +424,9 @@ private function checkSearchEngineViaDeploymentConfig($objectManager): ?string
469424
return ucfirst($engineConfig) . ' (Configured but not reachable)';
470425
}
471426
} catch (\Exception $e) {
472-
// Ignore specific exceptions
427+
if ($this->io->isVerbose()) {
428+
$this->io->warning('Failed to read search engine from deployment config: ' . $e->getMessage());
429+
}
473430
}
474431

475432
return null;
@@ -492,7 +449,9 @@ private function checkSearchEngineViaEngineResolver($objectManager): ?string
492449
}
493450
}
494451
} catch (\Exception $e) {
495-
// Ignore specific exceptions
452+
if ($this->io->isVerbose()) {
453+
$this->io->warning('Failed to read search engine via resolver: ' . $e->getMessage());
454+
}
496455
}
497456

498457
return null;
@@ -533,7 +492,9 @@ private function checkSearchEngineConnections(): ?string
533492
}
534493
}
535494
} catch (\Exception $e) {
536-
// Ignore
495+
if ($this->io->isVerbose()) {
496+
$this->io->warning('Failed to check search engine connections: ' . $e->getMessage());
497+
}
537498
}
538499

539500
return null;
@@ -573,7 +534,8 @@ private function getSearchEngineHosts(): array
573534
/**
574535
* Format search engine version output
575536
*
576-
* @param array<string, mixed> $info
537+
* @param array $info
538+
* @phpstan-param array<string, mixed> $info
577539
* @return string
578540
*/
579541
private function formatSearchEngineVersion(array $info): string
@@ -607,7 +569,9 @@ private function testElasticsearchConnection(string $url)
607569
// No fallback to native approaches anymore - rely on Magento's HTTP client only
608570
// This avoids using discouraged functions
609571
} catch (\Exception $e) {
610-
// Ignore exceptions and return false
572+
if ($this->io->isVerbose()) {
573+
$this->io->warning('Search engine connection check failed: ' . $e->getMessage());
574+
}
611575
}
612576

613577
return false;
@@ -636,7 +600,9 @@ private function tryMagentoHttpClient(string $url): ?array
636600
}
637601
}
638602
} catch (\Exception $e) {
639-
// Ignore exceptions
603+
if ($this->io->isVerbose()) {
604+
$this->io->warning('HTTP client request failed for search engine: ' . $e->getMessage());
605+
}
640606
}
641607

642608
return null;
@@ -738,7 +704,9 @@ private function getMagentoEnvironmentValue(string $name): ?string
738704
return $serviceValue;
739705
}
740706
} catch (\Exception $e) {
741-
// Ignore exceptions
707+
if ($this->io->isVerbose()) {
708+
$this->io->warning('Failed to read Magento environment value: ' . $e->getMessage());
709+
}
742710
}
743711

744712
return null;
@@ -760,7 +728,9 @@ private function getValueFromDeploymentConfig($objectManager, string $name): ?st
760728
return (string)$envValue;
761729
}
762730
} catch (\Exception $e) {
763-
// Ignore exceptions
731+
if ($this->io->isVerbose()) {
732+
$this->io->warning('Failed to read environment from deployment config: ' . $e->getMessage());
733+
}
764734
}
765735

766736
return null;
@@ -785,7 +755,9 @@ private function getValueFromEnvironmentService($objectManager, string $name): ?
785755
}
786756
}
787757
} catch (\Exception $e) {
788-
// Ignore exceptions
758+
if ($this->io->isVerbose()) {
759+
$this->io->warning('Failed to read environment from service: ' . $e->getMessage());
760+
}
789761
}
790762

791763
return null;

src/Console/Command/System/VersionCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ private function getLatestVersion(): string
100100
return $data['tag_name'] ?? self::UNKNOWN_VERSION;
101101
}
102102
} catch (\Exception $e) {
103-
// Fall through to return unknown
103+
if ($this->io->isVerbose()) {
104+
$this->io->warning('Failed to fetch latest version: ' . $e->getMessage());
105+
}
104106
}
105107

106108
return self::UNKNOWN_VERSION;

src/Console/Command/Theme/TokensCommand.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,24 +208,15 @@ private function generateTokens(string $tailwindPath, string $themeCode, bool $i
208208
$this->io->text("Working directory: $tailwindPath");
209209
}
210210

211-
$currentDir = getcwd();
212-
if ($currentDir === false) {
213-
$this->io->error("Cannot determine current directory");
214-
return false;
215-
}
216-
chdir($tailwindPath);
217-
218211
try {
219212
if ($isVerbose) {
220213
$this->io->text('Running npx hyva-tokens...');
221214
}
222215

223-
$this->shell->execute('npx hyva-tokens');
224-
chdir($currentDir);
216+
$this->shell->execute('cd %s && npx hyva-tokens', [$tailwindPath]);
225217

226218
return true;
227219
} catch (\Exception $e) {
228-
chdir($currentDir);
229220
$this->io->error('Failed to generate Hyvä design tokens: ' . $e->getMessage());
230221
return false;
231222
}

0 commit comments

Comments
 (0)