1010
1111class DetectionStrategyFactory
1212{
13+ private const TYPE_DIRECTORY = 'directory ' ;
14+ private const TYPE_COMMAND = 'command ' ;
15+ private const TYPE_FILE = 'file ' ;
16+
1317 public function __construct (private readonly Container $ container )
1418 {
1519 }
@@ -23,9 +27,9 @@ public function make(string|array $type, array $config = []): DetectionStrategy
2327 }
2428
2529 return match ($ type ) {
26- ' directory ' => $ this ->container ->make (DirectoryDetectionStrategy::class),
27- ' command ' => $ this ->container ->make (CommandDetectionStrategy::class),
28- ' file ' => $ this ->container ->make (FileDetectionStrategy::class),
30+ self :: TYPE_DIRECTORY => $ this ->container ->make (DirectoryDetectionStrategy::class),
31+ self :: TYPE_COMMAND => $ this ->container ->make (CommandDetectionStrategy::class),
32+ self :: TYPE_FILE => $ this ->container ->make (FileDetectionStrategy::class),
2933 default => throw new InvalidArgumentException ("Unknown detection type: {$ type }" ),
3034 };
3135 }
@@ -39,24 +43,23 @@ public function makeFromConfig(array $config): DetectionStrategy
3943
4044 private function inferTypeFromConfig (array $ config ): string |array
4145 {
42- $ types = [];
43-
44- if ( isset ( $ config [ ' files ' ])) {
45- $ types [] = ' file ' ;
46- }
46+ $ typeMap = [
47+ ' files ' => self :: TYPE_FILE ,
48+ ' paths ' => self :: TYPE_DIRECTORY ,
49+ ' command ' => self :: TYPE_COMMAND ,
50+ ];
4751
48- if (isset ($ config ['paths ' ])) {
49- $ types [] = 'directory ' ;
50- }
51-
52- if (isset ($ config ['command ' ])) {
53- $ types [] = 'command ' ;
54- }
52+ $ types = collect ($ typeMap )
53+ ->only (array_keys ($ config ))
54+ ->values ()
55+ ->all ();
5556
5657 if (empty ($ types )) {
57- throw new InvalidArgumentException ('Cannot infer detection type from config keys. Expected one of: files, paths, command ' );
58+ throw new InvalidArgumentException (
59+ 'Cannot infer detection type from config keys. Expected one of: ' . collect ($ typeMap )->keys ()->join (', ' )
60+ );
5861 }
5962
60- return count ($ types ) === 1 ? $ types[ 0 ] : $ types ;
63+ return count ($ types ) > 1 ? $ types : reset ( $ types) ;
6164 }
6265}
0 commit comments