Skip to content

Commit 8e72745

Browse files
mnapoliclaude
andcommitted
Optimize dependency analyzer to read composer.json only once and simplify messaging
- Read and parse composer.json only once instead of multiple times - Simplify return type to just warnings array instead of warnings + suggestions - Improve messaging to be more direct and actionable - Add proper type checks for PHPStan compliance - Reduce file I/O operations for better performance 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c5300b2 commit 8e72745

File tree

2 files changed

+52
-80
lines changed

2 files changed

+52
-80
lines changed

src/Commands/Deploy.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5959
]);
6060

6161
// Analyze dependencies for optimization warnings
62-
$dependencyAnalysis = DependencyAnalyzer::analyzeComposerDependencies();
63-
if (!empty($dependencyAnalysis['warnings'])) {
62+
$dependencyWarnings = DependencyAnalyzer::analyzeComposerDependencies();
63+
if (!empty($dependencyWarnings)) {
6464
IO::writeln(['']);
65-
foreach ($dependencyAnalysis['warnings'] as $warning) {
65+
foreach ($dependencyWarnings as $warning) {
6666
IO::writeln(Styles::yellow('' . $warning));
6767
}
68-
foreach ($dependencyAnalysis['suggestions'] as $suggestion) {
69-
IO::writeln(Styles::gray(' ' . $suggestion));
70-
}
7168
IO::writeln(['']);
7269
}
7370

src/Helpers/DependencyAnalyzer.php

Lines changed: 49 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,129 +8,104 @@
88
class DependencyAnalyzer
99
{
1010
/**
11-
* @return array{warnings: string[], suggestions: string[]}
11+
* @return string[]
1212
*/
1313
public static function analyzeComposerDependencies(string $composerJsonPath = 'composer.json'): array
1414
{
1515
if (!file_exists($composerJsonPath)) {
16-
return ['warnings' => [], 'suggestions' => []];
16+
return [];
1717
}
1818

1919
try {
2020
$composerContent = file_get_contents($composerJsonPath);
2121
if ($composerContent === false) {
22-
return ['warnings' => [], 'suggestions' => []];
22+
return [];
2323
}
2424

2525
$composer = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
2626
if (!is_array($composer)) {
27-
return ['warnings' => [], 'suggestions' => []];
27+
return [];
2828
}
2929

3030
$warnings = [];
31-
$suggestions = [];
31+
$require = $composer['require'] ?? [];
32+
33+
if (!is_array($require)) {
34+
return [];
35+
}
3236

3337
// Check for AWS SDK
34-
$require = $composer['require'] ?? [];
35-
if (is_array($require) && isset($require['aws/aws-sdk-php'])) {
36-
$isOptimized = self::isAwsSdkOptimized($composerJsonPath);
38+
if (isset($require['aws/aws-sdk-php'])) {
39+
$isOptimized = self::isAwsSdkOptimized($composer);
3740
if (!$isOptimized) {
38-
$warnings[] = 'AWS SDK detected - this can significantly increase deployment size';
39-
$suggestions[] = 'Consider optimizing AWS SDK by including only required services: https://github.com/aws/aws-sdk-php/tree/master/src/Script/Composer';
41+
$warnings[] = 'AWS SDK detected - optimize your deployment size: https://github.com/aws/aws-sdk-php/tree/master/src/Script/Composer';
4042
}
4143
}
4244

4345
// Check for Google SDK
44-
if (is_array($require) && (isset($require['google/apiclient']) || isset($require['google/cloud']))) {
45-
$isOptimized = self::isGoogleSdkOptimized($composerJsonPath);
46+
if (isset($require['google/apiclient']) || isset($require['google/cloud'])) {
47+
$isOptimized = self::isGoogleSdkOptimized($composer);
4648
if (!$isOptimized) {
47-
$warnings[] = 'Google SDK detected - this can significantly increase deployment size';
48-
$suggestions[] = 'Consider optimizing Google SDK by removing unused services: https://github.com/googleapis/google-api-php-client#cleaning-up-unused-services';
49+
$warnings[] = 'Google SDK detected - optimize your deployment size: https://github.com/googleapis/google-api-php-client#cleaning-up-unused-services';
4950
}
5051
}
5152

52-
return ['warnings' => $warnings, 'suggestions' => $suggestions];
53+
return $warnings;
5354

5455
} catch (JsonException) {
55-
return ['warnings' => [], 'suggestions' => []];
56+
return [];
5657
}
5758
}
5859

5960
/**
6061
* Check if AWS SDK is optimized by looking for custom scripts or exclusions
62+
* @param array<string, mixed> $composer
6163
*/
62-
private static function isAwsSdkOptimized(string $composerJsonPath): bool
64+
private static function isAwsSdkOptimized(array $composer): bool
6365
{
64-
try {
65-
$composerContent = file_get_contents($composerJsonPath);
66-
if ($composerContent === false) {
67-
return false;
68-
}
69-
70-
$composer = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
71-
if (!is_array($composer)) {
72-
return false;
73-
}
74-
75-
// Check for AWS SDK optimization script
76-
$scripts = $composer['scripts'] ?? [];
77-
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
78-
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
79-
foreach ($preAutoloadDump as $script) {
80-
if (is_string($script) && str_contains($script, 'aws-sdk-php') && str_contains($script, 'remove-unused-services')) {
81-
return true;
82-
}
66+
// Check for AWS SDK optimization script
67+
$scripts = $composer['scripts'] ?? [];
68+
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
69+
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
70+
foreach ($preAutoloadDump as $script) {
71+
if (is_string($script) && str_contains($script, 'aws-sdk-php') && str_contains($script, 'remove-unused-services')) {
72+
return true;
8373
}
8474
}
75+
}
8576

86-
// Check for custom AWS SDK optimizations in extra section
87-
if (isset($composer['extra']['aws']['includes'])) {
88-
return true;
89-
}
90-
91-
return false;
92-
93-
} catch (Exception) {
94-
return false;
77+
// Check for custom AWS SDK optimizations in extra section
78+
$extra = $composer['extra'] ?? [];
79+
if (is_array($extra) && isset($extra['aws']) && is_array($extra['aws']) && isset($extra['aws']['includes'])) {
80+
return true;
9581
}
82+
83+
return false;
9684
}
9785

9886
/**
9987
* Check if Google SDK is optimized by looking for custom exclusions
88+
* @param array<string, mixed> $composer
10089
*/
101-
private static function isGoogleSdkOptimized(string $composerJsonPath): bool
90+
private static function isGoogleSdkOptimized(array $composer): bool
10291
{
103-
try {
104-
$composerContent = file_get_contents($composerJsonPath);
105-
if ($composerContent === false) {
106-
return false;
107-
}
108-
109-
$composer = json_decode($composerContent, true, 512, JSON_THROW_ON_ERROR);
110-
if (!is_array($composer)) {
111-
return false;
112-
}
113-
114-
// Check for Google SDK optimization script
115-
$scripts = $composer['scripts'] ?? [];
116-
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
117-
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
118-
foreach ($preAutoloadDump as $script) {
119-
if (is_string($script) && str_contains($script, 'google') && str_contains($script, 'remove-unused-services')) {
120-
return true;
121-
}
92+
// Check for Google SDK optimization script
93+
$scripts = $composer['scripts'] ?? [];
94+
if (is_array($scripts) && isset($scripts['pre-autoload-dump'])) {
95+
$preAutoloadDump = (array) $scripts['pre-autoload-dump'];
96+
foreach ($preAutoloadDump as $script) {
97+
if (is_string($script) && str_contains($script, 'google') && str_contains($script, 'remove-unused-services')) {
98+
return true;
12299
}
123100
}
101+
}
124102

125-
// Check for custom Google SDK optimizations in extra section
126-
if (isset($composer['extra']['google']['exclude_files'])) {
127-
return true;
128-
}
129-
130-
return false;
131-
132-
} catch (Exception) {
133-
return false;
103+
// Check for custom Google SDK optimizations in extra section
104+
$extra = $composer['extra'] ?? [];
105+
if (is_array($extra) && isset($extra['google']) && is_array($extra['google']) && isset($extra['google']['exclude_files'])) {
106+
return true;
134107
}
108+
109+
return false;
135110
}
136111
}

0 commit comments

Comments
 (0)