14
14
15
15
namespace Humbug \PhpScoper \Configuration ;
16
16
17
+ use Humbug \PhpScoper \Configuration \Throwable \InvalidConfiguration ;
18
+ use Humbug \PhpScoper \Configuration \Throwable \InvalidConfigurationFile ;
19
+ use Humbug \PhpScoper \Configuration \Throwable \InvalidConfigurationValue ;
17
20
use Humbug \PhpScoper \Patcher \ComposerPatcher ;
18
21
use Humbug \PhpScoper \Patcher \Patcher ;
19
22
use Humbug \PhpScoper \Patcher \PatcherChain ;
20
23
use Humbug \PhpScoper \Patcher \SymfonyParentTraitPatcher ;
21
24
use Humbug \PhpScoper \Patcher \SymfonyPatcher ;
22
- use InvalidArgumentException ;
23
- use RuntimeException ;
24
25
use SplFileInfo ;
25
26
use Symfony \Component \Filesystem \Filesystem ;
26
27
use Symfony \Component \Finder \Finder ;
33
34
use function bin2hex ;
34
35
use function dirname ;
35
36
use function file_exists ;
36
- use function gettype ;
37
37
use function Humbug \PhpScoper \chain ;
38
- use function in_array ;
39
38
use function is_array ;
40
39
use function is_callable ;
41
40
use function is_dir ;
47
46
use function readlink as native_readlink ;
48
47
use function realpath ;
49
48
use function Safe \file_get_contents ;
50
- use function sprintf ;
51
49
use function trim ;
52
50
use const DIRECTORY_SEPARATOR ;
53
51
@@ -64,14 +62,12 @@ public function __construct(
64
62
/**
65
63
* @param non-empty-string|null $path Absolute canonical path to the configuration file.
66
64
* @param list<non-empty-string> $paths List of absolute canonical paths to append besides the one configured
65
+ *
66
+ * @throws InvalidConfiguration
67
67
*/
68
68
public function create (?string $ path = null , array $ paths = []): Configuration
69
69
{
70
- if (null === $ path ) {
71
- $ config = [];
72
- } else {
73
- $ config = $ this ->loadConfigFile ($ path );
74
- }
70
+ $ config = null === $ path ? [] : $ this ->loadConfigFile ($ path );
75
71
76
72
self ::validateConfigKeys ($ config );
77
73
@@ -134,48 +130,31 @@ public function createWithPrefix(Configuration $config, string $prefix): Configu
134
130
return $ config ->withPrefix ($ prefix );
135
131
}
136
132
133
+ /**
134
+ * @throws InvalidConfigurationValue
135
+ */
137
136
private function loadConfigFile (string $ path ): array
138
137
{
139
138
if (!$ this ->fileSystem ->isAbsolutePath ($ path )) {
140
- throw new InvalidArgumentException (
141
- sprintf (
142
- 'Expected the path of the configuration file to load to be an absolute path, got "%s" instead ' ,
143
- $ path ,
144
- ),
145
- );
139
+ throw InvalidConfigurationFile::forNonAbsolutePath ($ path );
146
140
}
147
141
148
142
if (!file_exists ($ path )) {
149
- throw new InvalidArgumentException (
150
- sprintf (
151
- 'Expected the path of the configuration file to exists but the file "%s" could not be found ' ,
152
- $ path ,
153
- ),
154
- );
143
+ throw InvalidConfigurationFile::forFileNotFound ($ path );
155
144
}
156
145
157
146
$ isADirectoryLink = is_link ($ path )
158
147
&& false !== native_readlink ($ path )
159
148
&& is_file (native_readlink ($ path ));
160
149
161
150
if (!$ isADirectoryLink && !is_file ($ path )) {
162
- throw new InvalidArgumentException (
163
- sprintf (
164
- 'Expected the path of the configuration file to be a file but "%s" appears to be a directory. ' ,
165
- $ path ,
166
- ),
167
- );
151
+ throw InvalidConfigurationFile::forNotAFile ($ path );
168
152
}
169
153
170
154
$ config = include $ path ;
171
155
172
156
if (!is_array ($ config )) {
173
- throw new InvalidArgumentException (
174
- sprintf (
175
- 'Expected configuration to be an array, found "%s" instead. ' ,
176
- gettype ($ config ),
177
- ),
178
- );
157
+ throw InvalidConfigurationFile::forInvalidValue ($ path );
179
158
}
180
159
181
160
return $ config ;
@@ -184,25 +163,11 @@ private function loadConfigFile(string $path): array
184
163
private static function validateConfigKeys (array $ config ): void
185
164
{
186
165
array_map (
187
- static fn ( string $ key ) => self :: validateConfigKey ( $ key ),
166
+ ConfigurationKeys:: assertIsValidKey (... ),
188
167
array_keys ($ config ),
189
168
);
190
169
}
191
170
192
- private static function validateConfigKey (string $ key ): void
193
- {
194
- if (in_array ($ key , ConfigurationKeys::KEYWORDS , true )) {
195
- return ;
196
- }
197
-
198
- throw new InvalidArgumentException (
199
- sprintf (
200
- 'Invalid configuration key value "%s" found. ' ,
201
- $ key ,
202
- ),
203
- );
204
- }
205
-
206
171
/**
207
172
* @return non-empty-string
208
173
*/
@@ -224,6 +189,8 @@ private static function retrieveOutputDir(array $config): ?string
224
189
}
225
190
226
191
/**
192
+ * @throws InvalidConfigurationValue
193
+ *
227
194
* @return array<(callable(string,string,string): string)|Patcher>
228
195
*/
229
196
private static function retrievePatchers (array $ config ): array
@@ -235,31 +202,21 @@ private static function retrievePatchers(array $config): array
235
202
$ patchers = $ config [ConfigurationKeys::PATCHERS_KEYWORD ];
236
203
237
204
if (!is_array ($ patchers )) {
238
- throw new InvalidArgumentException (
239
- sprintf (
240
- 'Expected patchers to be an array of callables, found "%s" instead. ' ,
241
- gettype ($ patchers ),
242
- ),
243
- );
205
+ throw InvalidConfigurationValue::forInvalidPatchersType ($ patchers );
244
206
}
245
207
246
208
foreach ($ patchers as $ index => $ patcher ) {
247
- if (is_callable ($ patcher )) {
248
- continue ;
209
+ if (! is_callable ($ patcher )) {
210
+ throw InvalidConfigurationValue:: forInvalidPatcherType ( $ index , $ patcher ) ;
249
211
}
250
-
251
- throw new InvalidArgumentException (
252
- sprintf (
253
- 'Expected patchers to be an array of callables, the "%d" element is not. ' ,
254
- $ index ,
255
- ),
256
- );
257
212
}
258
213
259
214
return $ patchers ;
260
215
}
261
216
262
217
/**
218
+ * @throws InvalidConfigurationValue
219
+ *
263
220
* @return string[] Absolute paths
264
221
*/
265
222
private function retrieveExcludedFiles (string $ dirPath , array $ config ): array
@@ -271,22 +228,12 @@ private function retrieveExcludedFiles(string $dirPath, array $config): array
271
228
$ excludedFiles = $ config [ConfigurationKeys::EXCLUDED_FILES_KEYWORD ];
272
229
273
230
if (!is_array ($ excludedFiles )) {
274
- throw new InvalidArgumentException (
275
- sprintf (
276
- 'Expected excluded files to be an array of strings, found "%s" instead. ' ,
277
- gettype ($ excludedFiles ),
278
- ),
279
- );
231
+ throw InvalidConfigurationValue::forInvalidExcludedFilesTypes ($ excludedFiles );
280
232
}
281
233
282
234
foreach ($ excludedFiles as $ index => $ file ) {
283
235
if (!is_string ($ file )) {
284
- throw new InvalidArgumentException (
285
- sprintf (
286
- 'Expected excluded files to be an array of string, the "%d" element is not. ' ,
287
- $ index ,
288
- ),
289
- );
236
+ throw InvalidConfigurationValue::forInvalidExcludedFilePath ($ index , $ excludedFiles );
290
237
}
291
238
292
239
if (!$ this ->fileSystem ->isAbsolutePath ($ file )) {
@@ -296,10 +243,14 @@ private function retrieveExcludedFiles(string $dirPath, array $config): array
296
243
$ excludedFiles [$ index ] = realpath ($ file );
297
244
}
298
245
246
+ // We ignore files not found excluded file as we do not want to bail out just because a file we do not want to
247
+ // include does not exist.
299
248
return array_filter ($ excludedFiles );
300
249
}
301
250
302
251
/**
252
+ * @throws InvalidConfigurationValue
253
+ *
303
254
* @return Finder[]
304
255
*/
305
256
private static function retrieveFinders (array $ config ): array
@@ -311,27 +262,15 @@ private static function retrieveFinders(array $config): array
311
262
$ finders = $ config [ConfigurationKeys::FINDER_KEYWORD ];
312
263
313
264
if (!is_array ($ finders )) {
314
- throw new InvalidArgumentException (
315
- sprintf (
316
- 'Expected finders to be an array of "%s", found "%s" instead. ' ,
317
- Finder::class,
318
- gettype ($ finders ),
319
- ),
320
- );
265
+ throw InvalidConfigurationValue::forInvalidFinderTypes ($ finders );
321
266
}
322
267
323
268
foreach ($ finders as $ index => $ finder ) {
324
269
if ($ finder instanceof Finder) {
325
270
continue ;
326
271
}
327
272
328
- throw new InvalidArgumentException (
329
- sprintf (
330
- 'Expected finders to be an array of "%s", the "%d" element is not. ' ,
331
- Finder::class,
332
- $ index ,
333
- ),
334
- );
273
+ throw InvalidConfigurationValue::forInvalidFinderType ($ index , $ finder );
335
274
}
336
275
337
276
return $ finders ;
@@ -340,6 +279,8 @@ private static function retrieveFinders(array $config): array
340
279
/**
341
280
* @param string[] $paths
342
281
*
282
+ * @throws InvalidConfigurationValue
283
+ *
343
284
* @return iterable<SplFileInfo>
344
285
*/
345
286
private static function retrieveFilesFromPaths (array $ paths ): iterable
@@ -353,12 +294,7 @@ private static function retrieveFilesFromPaths(array $paths): iterable
353
294
354
295
foreach ($ paths as $ path ) {
355
296
if (!file_exists ($ path )) {
356
- throw new RuntimeException (
357
- sprintf (
358
- 'Could not find the file "%s". ' ,
359
- $ path ,
360
- ),
361
- );
297
+ throw InvalidConfigurationValue::forFileNotFound ($ path );
362
298
}
363
299
364
300
if (is_dir ($ path )) {
@@ -384,6 +320,8 @@ private static function retrieveFilesFromPaths(array $paths): iterable
384
320
/**
385
321
* @param iterable<SplFileInfo|string> $files
386
322
*
323
+ * @throws InvalidConfigurationValue
324
+ *
387
325
* @return array<string, array{string, string}> Array of tuple with the first argument being the file path and the second its contents
388
326
*/
389
327
private static function retrieveFilesWithContents (iterable $ files ): array
@@ -396,21 +334,11 @@ private static function retrieveFilesWithContents(iterable $files): array
396
334
: realpath ($ filePathOrFileInfo );
397
335
398
336
if (!$ filePath ) {
399
- throw new RuntimeException (
400
- sprintf (
401
- 'Could not find the file "%s". ' ,
402
- (string ) $ filePathOrFileInfo ,
403
- ),
404
- );
337
+ throw InvalidConfigurationValue::forFileNotFound ((string ) $ filePathOrFileInfo );
405
338
}
406
339
407
340
if (!is_readable ($ filePath )) {
408
- throw new RuntimeException (
409
- sprintf (
410
- 'Could not read the file "%s". ' ,
411
- $ filePath ,
412
- ),
413
- );
341
+ throw InvalidConfigurationValue::forUnreadableFile ($ filePath );
414
342
}
415
343
416
344
$ filesWithContents [$ filePath ] = [$ filePath , file_get_contents ($ filePath )];
0 commit comments