1919use Iterator ;
2020use RuntimeException ;
2121use SplFileInfo ;
22+ use Symfony \Component \Filesystem \Filesystem ;
2223use Symfony \Component \Finder \Finder ;
2324use const DIRECTORY_SEPARATOR ;
2425use function dirname ;
26+ use function file_exists ;
2527use function gettype ;
28+ use function in_array ;
2629use function is_array ;
2730use function is_bool ;
31+ use function is_file ;
32+ use function is_link ;
2833use function is_string ;
34+ use function readlink ;
2935use function realpath ;
36+ use function sprintf ;
3037
3138/**
3239 * @final
@@ -72,6 +79,36 @@ public static function load(string $path = null, array $paths = []): self
7279 if (null === $ path ) {
7380 $ config = [];
7481 } else {
82+ if (false === (new Filesystem ())->isAbsolutePath ($ path )) {
83+ throw new InvalidArgumentException (
84+ sprintf (
85+ 'Expected the path of the configuration file to load to be an absolute path, got "%s" '
86+ .'instead ' ,
87+ $ path
88+ )
89+ );
90+ }
91+
92+ if (false === file_exists ($ path )) {
93+ throw new InvalidArgumentException (
94+ sprintf (
95+ 'Expected the path of the configuration file to exists but the file "%s" could not be '
96+ .'found ' ,
97+ $ path
98+ )
99+ );
100+ }
101+
102+ if (false === is_file ($ path ) && false === (is_link ($ path ) && is_file (readlink ($ path )))) {
103+ throw new InvalidArgumentException (
104+ sprintf (
105+ 'Expected the path of the configuration file to be a file but "%s" appears to be a '
106+ .'directory. ' ,
107+ $ path
108+ )
109+ );
110+ }
111+
75112 $ config = include $ path ;
76113
77114 if (false === is_array ($ config )) {
@@ -162,7 +199,7 @@ public function withPrefix(?string $prefix): self
162199 );
163200 }
164201
165- public function getPath (): string
202+ public function getPath (): ? string
166203 {
167204 return $ this ->path ;
168205 }
@@ -208,7 +245,7 @@ private static function validateConfigKeys(array $config): void
208245
209246 private static function validateConfigKey (string $ key ): void
210247 {
211- if (false === in_array ($ key , self ::KEYWORDS )) {
248+ if (false === in_array ($ key , self ::KEYWORDS , true )) {
212249 throw new InvalidArgumentException (
213250 sprintf (
214251 'Invalid configuration key value "%s" found. ' ,
@@ -360,7 +397,7 @@ private static function retrieveWhitelistedFiles(string $dirPath, array $config)
360397 return [];
361398 }
362399
363- $ whitelistedFiles = $ config [self ::WHITELIST_KEYWORD ];
400+ $ whitelistedFiles = $ config [self ::WHITELISTED_FILES_KEYWORD ];
364401
365402 if (false === is_array ($ whitelistedFiles )) {
366403 throw new InvalidArgumentException (
@@ -372,7 +409,7 @@ private static function retrieveWhitelistedFiles(string $dirPath, array $config)
372409 }
373410
374411 foreach ($ whitelistedFiles as $ index => $ file ) {
375- if (is_string ($ file )) {
412+ if (false === is_string ($ file )) {
376413 throw new InvalidArgumentException (
377414 sprintf (
378415 'Expected whitelisted files to be an array of string, the "%d" element is not. ' ,
@@ -381,7 +418,7 @@ private static function retrieveWhitelistedFiles(string $dirPath, array $config)
381418 );
382419 }
383420
384- if ('' !== $ file && DIRECTORY_SEPARATOR !== $ file[ 0 ] ) {
421+ if (false === ( new Filesystem ())-> isAbsolutePath ( $ file) ) {
385422 $ file = $ dirPath .DIRECTORY_SEPARATOR .$ file ;
386423 }
387424
0 commit comments