44
55use GrumPHP \Runner \TaskResult ;
66use GrumPHP \Runner \TaskResultInterface ;
7- use GrumPHP \Task \AbstractExternalTask ;
7+ use GrumPHP \Task \Config \EmptyTaskConfig ;
8+ use GrumPHP \Task \Config \TaskConfigInterface ;
89use GrumPHP \Task \Context \ContextInterface ;
910use GrumPHP \Task \Context \GitPreCommitContext ;
1011use GrumPHP \Task \Context \RunContext ;
12+ use GrumPHP \Task \TaskInterface ;
1113use Symfony \Component \OptionsResolver \OptionsResolver ;
1214
13- class BomFixerTask extends AbstractExternalTask
15+ final class BomFixerTask implements TaskInterface
1416{
15- public function getName (): string
17+ /** @var TaskConfigInterface */
18+ private $ config ;
19+
20+ public function __construct ()
1621 {
17- return ' plus_bom_fixer ' ;
22+ $ this -> config = new EmptyTaskConfig () ;
1823 }
1924
20- public function getConfigurableOptions (): OptionsResolver
25+ public static function getConfigurableOptions (): OptionsResolver
2126 {
2227 $ resolver = new OptionsResolver ();
2328 $ resolver ->setDefaults (
@@ -31,15 +36,27 @@ public function getConfigurableOptions(): OptionsResolver
3136 return $ resolver ;
3237 }
3338
39+ public function getConfig (): TaskConfigInterface
40+ {
41+ return $ this ->config ;
42+ }
43+
44+ public function withConfig (TaskConfigInterface $ config ): TaskInterface
45+ {
46+ $ new = clone $ this ;
47+ $ new ->config = $ config ;
48+
49+ return $ new ;
50+ }
51+
3452 public function canRunInContext (ContextInterface $ context ): bool
3553 {
36- return ( $ context instanceof GitPreCommitContext || $ context instanceof RunContext) ;
54+ return $ context instanceof RunContext || $ context instanceof GitPreCommitContext ;
3755 }
3856
3957 public function run (ContextInterface $ context ): TaskResultInterface
4058 {
41- $ config = $ this ->getConfiguration ();
42- $ files = $ context ->getFiles ()->extensions ($ config ['triggered_by ' ]);
59+ $ files = $ context ->getFiles ()->extensions ($ this ->config ->getOptions ()['triggered_by ' ]);
4360 if (0 === count ($ files )) {
4461 return TaskResult::createSkipped ($ this , $ context );
4562 }
@@ -64,18 +81,20 @@ public function run(ContextInterface $context): TaskResultInterface
6481 }
6582
6683 if (count ($ shouldGetFixedLog ) > 0 ) {
67- return TaskResult::createFailed (
68- $ this ,
69- $ context ,
70- implode (PHP_EOL , $ shouldGetFixedLog ) . PHP_EOL
84+ $ errorMessage = implode (PHP_EOL , $ shouldGetFixedLog ) . PHP_EOL
7185 . 'you can use this to fix them: ' . PHP_EOL
72- . $ fixCommand
73- );
86+ . $ fixCommand;
87+ return TaskResult:: createFailed ( $ this , $ context , $ errorMessage );
7488 }
7589 return TaskResult::createPassed ($ this , $ context );
7690 }
7791
78- protected function fileInfoSearch (string $ filename , string $ search ): bool
92+ private function isFileWithBOM (string $ filename ): bool
93+ {
94+ return $ this ->fileInfoSearch ($ filename , 'BOM ' );
95+ }
96+
97+ private function fileInfoSearch (string $ filename , string $ search ): bool
7998 {
8099 $ output = [];
81100 exec ('file ' . '" ' . $ filename . '" ' , $ output , $ returnVar );
@@ -84,9 +103,4 @@ protected function fileInfoSearch(string $filename, string $search): bool
84103 }
85104 return false ;
86105 }
87-
88- public function isFileWithBOM (string $ filename ): bool
89- {
90- return $ this ->fileInfoSearch ($ filename , 'BOM ' );
91- }
92106}
0 commit comments