1+ <?php declare (strict_types=1 );
2+
3+ namespace Bref \Cli \Helpers ;
4+
5+ use Exception ;
6+ use JsonException ;
7+
8+ class DependencyAnalyzer
9+ {
10+ /**
11+ * @return string[]
12+ */
13+ public static function analyzeComposerDependencies (string $ composerJsonPath = 'composer.json ' ): array
14+ {
15+ if (!file_exists ($ composerJsonPath )) {
16+ return [];
17+ }
18+
19+ try {
20+ $ composerContent = file_get_contents ($ composerJsonPath );
21+ if ($ composerContent === false ) {
22+ return [];
23+ }
24+
25+ $ composer = json_decode ($ composerContent , true , 512 , JSON_THROW_ON_ERROR );
26+ if (!is_array ($ composer )) {
27+ return [];
28+ }
29+
30+ $ warnings = [];
31+ $ require = $ composer ['require ' ] ?? [];
32+
33+ if (!is_array ($ require )) {
34+ return [];
35+ }
36+
37+ // Check for AWS SDK in composer.json or vendor directory
38+ $ hasAwsSdk = isset ($ require ['aws/aws-sdk-php ' ]) ||
39+ (is_dir ('vendor/aws/aws-sdk-php ' ) && file_exists ('vendor/aws/aws-sdk-php/composer.json ' ));
40+
41+ if ($ hasAwsSdk ) {
42+ $ isOptimized = self ::isAwsSdkOptimized ($ composer );
43+ if (!$ isOptimized ) {
44+ $ warnings [] = 'AWS SDK detected - optimize your deployment size: https://github.com/aws/aws-sdk-php/tree/master/src/Script/Composer ' ;
45+ }
46+ }
47+
48+ // Check for Google SDK in composer.json or vendor directory
49+ $ hasGoogleSdk = isset ($ require ['google/apiclient ' ]) ||
50+ (is_dir ('vendor/google/apiclient ' ) && file_exists ('vendor/google/apiclient/composer.json ' ));
51+
52+ if ($ hasGoogleSdk ) {
53+ $ isOptimized = self ::isGoogleSdkOptimized ($ composer );
54+ if (!$ isOptimized ) {
55+ $ warnings [] = 'Google SDK detected - optimize your deployment size: https://github.com/googleapis/google-api-php-client#cleaning-up-unused-services ' ;
56+ }
57+ }
58+
59+ return $ warnings ;
60+
61+ } catch (JsonException ) {
62+ return [];
63+ }
64+ }
65+
66+ /**
67+ * Check if AWS SDK is optimized by looking for custom scripts or exclusions
68+ * @param array<string, mixed> $composer
69+ */
70+ private static function isAwsSdkOptimized (array $ composer ): bool
71+ {
72+ // Check for AWS SDK optimization script
73+ $ scripts = $ composer ['scripts ' ] ?? [];
74+ if (is_array ($ scripts ) && isset ($ scripts ['pre-autoload-dump ' ])) {
75+ $ preAutoloadDump = (array ) $ scripts ['pre-autoload-dump ' ];
76+ foreach ($ preAutoloadDump as $ script ) {
77+ if (is_string ($ script ) && str_contains ($ script , 'Aws \\Script \\Composer \\Composer::removeUnusedServices ' )) {
78+ return true ;
79+ }
80+ }
81+ }
82+
83+ return false ;
84+ }
85+
86+ /**
87+ * Check if Google SDK is optimized by looking for custom exclusions
88+ * @param array<string, mixed> $composer
89+ */
90+ private static function isGoogleSdkOptimized (array $ composer ): bool
91+ {
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 \\Task \\Composer::cleanup ' )) {
98+ return true ;
99+ }
100+ }
101+ }
102+
103+ return false ;
104+ }
105+ }
0 commit comments