Skip to content

Commit 2941719

Browse files
committed
Changed header comment setup in FixerFactory
1 parent 3979503 commit 2941719

File tree

5 files changed

+61
-29
lines changed

5 files changed

+61
-29
lines changed

cs-fixer.php.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212
use Polymorphine\Dev\FixerFactory;
1313

14-
return FixerFactory::createFor('Polymorphine/Dev', __DIR__);
14+
return FixerFactory::createFor(__FILE__);

src/FixerFactory.php

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818

1919
final 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
}

template/cs-fixer.php.dist.sk_file

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
/*
44
* This file is part of {package.name} package.
55
*
6-
* (c) Shudd3r <q3.shudder@gmail.com>
6+
* (c) {author.name} <{author.email}>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.
1010
*/
1111

1212
use Polymorphine\Dev\FixerFactory;
1313

14-
return FixerFactory::createFor('{package.name}', __DIR__);
14+
return FixerFactory::createFor(__FILE__);

tests/CompoundFixerTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
use Polymorphine\Dev\Tests\Fixtures\FixerTestRunner;
1717

1818

19-
/**
20-
* @group integrated
21-
*/
19+
/** @group integrated */
2220
class CompoundFixerTest extends TestCase
2321
{
2422
private FixerTestRunner $runner;
2523

2624
protected function setUp(): void
2725
{
28-
$config = FixerFactory::createFor('Polymorphine/Dev', __DIR__);
26+
$config = FixerFactory::createFor(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'cs-fixer.php.dist');
2927
$this->runner = FixerTestRunner::withConfig($config);
3028
}
3129

tests/FixerFactoryTest.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,47 @@ class FixerFactoryTest extends TestCase
2020
{
2121
public function testConfigInstantiation()
2222
{
23-
$this->assertInstanceOf(ConfigInterface::class, FixerFactory::createFor('package/name', __DIR__));
23+
$this->assertInstanceOf(ConfigInterface::class, FixerFactory::createFor(__FILE__));
2424
}
2525

2626
public function testConfigFinder_IgnoresCodeSamples()
2727
{
28-
$excluded = __DIR__ . str_replace('/', DIRECTORY_SEPARATOR, '/Fixtures/code-samples');
29-
$finder = FixerFactory::createFor('package/name', dirname(__DIR__))->getFinder();
28+
$finder = FixerFactory::createFor($this->packagePath('cs-fixer.php.dist'))->getFinder();
3029

3130
$this->assertTrue($finder->hasResults());
3231

32+
$excludedPath = $this->packagePath('tests/Fixtures/code-samples');
3333
foreach ($finder->getIterator() as $file) {
34-
$this->assertFalse(strpos($file->getPath(), $excluded));
34+
$this->assertStringNotContainsString($excludedPath, $file->getPath());
3535
}
3636
}
37+
38+
public function testHeaderIsReadFromLaunchFile()
39+
{
40+
$expectedHeader = <<<'HEADER'
41+
This file is part of Polymorphine/Dev package.
42+
43+
(c) Shudd3r <q3.shudder@gmail.com>
44+
45+
This source file is subject to the MIT license that is bundled
46+
with this source code in the file LICENSE.
47+
HEADER;
48+
49+
$rules = FixerFactory::createFor($this->packagePath('cs-fixer.php.dist'))->getRules();
50+
$this->assertSame($expectedHeader, $rules['header_comment']['header']);
51+
52+
$file = $this->packagePath('tests/Fixtures/code-samples/Fixer/given-global.php');
53+
$rules = FixerFactory::createFor($file)->getRules();
54+
$this->assertSame('LOL surprise comment!', $rules['header_comment']['header']);
55+
56+
$file = $this->packagePath('tests/Fixtures/code-samples/Fixer/given-ExampleClass.php');
57+
$rules = FixerFactory::createFor($file)->getRules();
58+
$this->assertFalse($rules['header_comment']);
59+
}
60+
61+
private function packagePath(string $relativePath): string
62+
{
63+
$relativePath = str_replace('/', DIRECTORY_SEPARATOR, trim($relativePath, DIRECTORY_SEPARATOR));
64+
return dirname(__DIR__) . DIRECTORY_SEPARATOR . $relativePath;
65+
}
3766
}

0 commit comments

Comments
 (0)