1818
1919final class FixerFactory
2020{
21- public const HEADER = <<<'EOF'
22- This file is part of {{name}} package.
23-
24- (c) Shudd3r <q3.shudder@gmail.com>
25-
26- This source file is subject to the MIT license that is bundled
27- with this source code in the file LICENSE.
28- EOF;
29-
3021 private static array $ rules = [
3122 '@Symfony ' => true ,
3223 'align_multiline_comment ' => true ,
@@ -42,7 +33,6 @@ final class FixerFactory
4233 'explicit_string_variable ' => false ,
4334 'final_internal_class ' => true ,
4435 'function_to_constant ' => true ,
45- 'header_comment ' => ['comment_type ' => 'comment ' ],
4636 'heredoc_to_nowdoc ' => true ,
4737 'increment_style ' => false ,
4838 'list_syntax ' => ['syntax ' => 'short ' ],
@@ -83,15 +73,12 @@ final class FixerFactory
8373 'yoda_style ' => false
8474 ];
8575
86- /**
87- * @param string $packageName
88- * @param string $workingDir
89- *
90- * @return Config
91- */
92- public static function createFor (string $ packageName , string $ workingDir ): Config
76+ public static function createFor (string $ launchFile ): Config
9377 {
94- self ::$ rules ['header_comment ' ]['header ' ] = str_replace ('{{name}} ' , $ packageName , self ::HEADER );
78+ $ workingDir = dirname ($ launchFile );
79+
80+ self ::setHeaderFrom ($ launchFile );
81+
9582 self ::$ rules ['no_extra_blank_lines ' ]['tokens ' ] = [
9683 'break ' , 'continue ' , 'extra ' , 'return ' , 'throw ' , 'parenthesis_brace_block ' ,
9784 'square_brace_block ' , 'curly_brace_block '
@@ -134,4 +121,22 @@ public static function createFor(string $packageName, string $workingDir): Confi
134121 new Fixer \BraceAfterMultilineParamMethodFixer ()
135122 ]);
136123 }
124+
125+ private static function setHeaderFrom (string $ filename ): void
126+ {
127+ self ::$ rules ['header_comment ' ] = false ;
128+
129+ $ contents = file_get_contents ($ filename ) ?: '' ;
130+ $ headerStart = strpos ($ contents , "\n/* \n" );
131+ $ headerEnd = strpos ($ contents , "\n */ \n" );
132+ if (!$ headerStart || !$ headerEnd ) { return ; }
133+
134+ $ header = substr ($ contents , $ headerStart + 4 , $ headerEnd - $ headerStart - 4 );
135+ if (!$ header ) { return ; }
136+
137+ self ::$ rules ['header_comment ' ] = [
138+ 'comment_type ' => 'comment ' ,
139+ 'header ' => str_replace ([' * ' , ' * ' ], '' , $ header )
140+ ];
141+ }
137142}
0 commit comments