Skip to content

Commit bf1c183

Browse files
authored
Merge pull request #6 from basz/feature/header-comment
header comment
2 parents fa8c0e1 + b2ac64e commit bf1c183

File tree

7 files changed

+92
-22
lines changed

7 files changed

+92
-22
lines changed

.docheader

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/**
2-
* This file is part of the prooph/php-cs-fixer-config.
3-
* (c) 2016-%year% prooph software GmbH <contact@prooph.de>
4-
* (c) 2016-%year% Sascha-Oliver Prolic <saschaprolic@googlemail.com>
5-
*
6-
* For the full copyright and license information, please view the LICENSE
7-
* file that was distributed with this source code.
8-
*/
1+
This file is part of `%package%`.
2+
(c) 2016-%year% prooph software GmbH <contact@prooph.de>
3+
(c) 2016-%year% Sascha-Oliver Prolic <saschaprolic@googlemail.com>
4+
5+
For the full copyright and license information, please view the LICENSE
6+
file that was distributed with this source code.

.php_cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<?php
2+
23
/**
3-
* This file is part of the prooph/php-cs-fixer-config.
4-
* (c) 2016-2016 prooph software GmbH <contact@prooph.de>
5-
* (c) 2016-2016 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
4+
* This file is part of `prooph/php-cs-fixer-config`.
5+
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
6+
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
67
*
78
* For the full copyright and license information, please view the LICENSE
89
* file that was distributed with this source code.
910
*/
1011

12+
declare(strict_types=1);
13+
1114
$config = new Prooph\CS\Config\Prooph();
1215
$config->getFinder()->in(__DIR__);
16+
$config->getFinder()->append(['.php_cs']);
1317

14-
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
18+
$cacheDir = \getenv('TRAVIS') ? \getenv('HOME') . '/.php-cs-fixer' : __DIR__;
1519

1620
$config->setCacheFile($cacheDir . '/.php_cs.cache');
1721

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ before_script:
3232
script:
3333
- if [[ $TEST_COVERAGE == 'true' ]]; then php -dzend_extension=xdebug.so ./vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml; else ./vendor/bin/phpunit; fi
3434
- ./vendor/bin/php-cs-fixer fix -v --diff --dry-run
35-
- ./vendor/bin/docheader check src/ tests/
3635

3736
after_success:
3837
- if [[ $TEST_COVERAGE == 'true' ]]; then php vendor/bin/coveralls -v; fi

README.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ Run
1414
```
1515
$ composer require --dev prooph/php-cs-fixer-config
1616
```
17+
18+
Add to composer.json;
19+
20+
```json
21+
"scripts": {
22+
"check": [
23+
"@cs",
24+
],
25+
"cs": "php-cs-fixer fix -v --diff --dry-run",
26+
"cs-fix": "php-cs-fixer fix -v --diff",
27+
}
28+
```
1729

1830
## Usage
1931

@@ -26,6 +38,7 @@ Create a configuration file `.php_cs` in the root of your project:
2638

2739
$config = new Prooph\CS\Config\Prooph();
2840
$config->getFinder()->in(__DIR__);
41+
$config->getFinder()->append(['.php_cs']);
2942

3043
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
3144

@@ -34,6 +47,21 @@ $config->setCacheFile($cacheDir . '/.php_cs.cache');
3447
return $config;
3548
```
3649

50+
#### Header
51+
52+
When you create a `.docheader` in the root of your project it will be used as header comment.
53+
54+
It is recommended to use the following template but you may use anything you want.
55+
56+
```
57+
This file is part of `%package%`.
58+
(c) 2016-%year% prooph software GmbH <contact@prooph.de>
59+
(c) 2016-%year% Sascha-Oliver Prolic <saschaprolic@googlemail.com>
60+
61+
For the full copyright and license information, please view the LICENSE
62+
file that was distributed with this source code.
63+
```
64+
3765
### Git
3866

3967
Add `.php_cs.cache` (this is the cache file created by `php-cs-fixer`) to `.gitignore`:
@@ -67,7 +95,7 @@ script:
6795
If you need to fix issues locally, just run
6896

6997
```
70-
$ ./vendor/bin/php-cs-fixer fix -v
98+
$ composer cs-fix
7199
```
72100
73101
### Pre-commit hook

composer.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
},
2121
"require-dev": {
2222
"phpunit/phpunit": "^6.0",
23-
"satooshi/php-coveralls": "^1.0",
24-
"malukenho/docheader": "^0.1.4"
23+
"satooshi/php-coveralls": "^1.0"
2524
},
2625
"autoload": {
2726
"psr-4": {
@@ -32,5 +31,14 @@
3231
"psr-4": {
3332
"ProophTest\\CS\\Config\\": "test"
3433
}
34+
},
35+
"scripts": {
36+
"check": [
37+
"@cs",
38+
"@test"
39+
],
40+
"cs": "php-cs-fixer fix -v --diff --dry-run",
41+
"cs-fix": "php-cs-fixer fix -v --diff",
42+
"test": "phpunit"
3543
}
3644
}

src/Prooph.php

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
2+
23
/**
3-
* This file is part of the prooph/php-cs-fixer-config.
4+
* This file is part of `prooph/php-cs-fixer-config`.
45
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
56
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
67
*
@@ -25,7 +26,7 @@ public function __construct()
2526

2627
public function getRules(): array
2728
{
28-
return [
29+
$rules = [
2930
'@PSR2' => true,
3031
'array_syntax' => ['syntax' => 'short'],
3132
'binary_operator_spaces' => [
@@ -47,7 +48,12 @@ public function getRules(): array
4748
'function_declaration' => true,
4849
'function_typehint_space' => true,
4950
'hash_to_slash_comment' => true,
50-
'header_comment' => false,
51+
'header_comment' => [
52+
'commentType' => 'PHPDoc',
53+
'header' => 'Prooph was here at `%package%` in `%year%`! Please create a .docheader in the project root and run `composer cs-fix`',
54+
'location' => 'after_open',
55+
'separate' => 'both',
56+
],
5157
'include' => true,
5258
'indentation_type' => true,
5359
'linebreak_after_opening_tag' => true,
@@ -111,5 +117,32 @@ public function getRules(): array
111117
'visibility_required' => true,
112118
'whitespace_after_comma_in_array' => true,
113119
];
120+
121+
$rules['header_comment'] = $this->headerComment($rules['header_comment']);
122+
123+
return $rules;
124+
}
125+
126+
private function headerComment(array $rules): array
127+
{
128+
if (\file_exists('.docheader')) {
129+
$header = \file_get_contents('.docheader');
130+
} else {
131+
$header = $rules['header'];
132+
}
133+
134+
// remove comments from existing .docheader or crash
135+
$header = \str_replace(['/**', ' */', ' * ', ' *'], '', $header);
136+
$package = 'unknown';
137+
138+
if (\file_exists('composer.json')) {
139+
$package = \json_decode(\file_get_contents('composer.json'))->name;
140+
}
141+
142+
$header = \str_replace(['%package%', '%year%'], [$package, (new \DateTime('now'))->format('Y')], $header);
143+
144+
$rules['header'] = \trim($header);
145+
146+
return $rules;
114147
}
115148
}

tests/ProophTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
2+
23
/**
3-
* This file is part of the prooph/php-cs-fixer-config.
4+
* This file is part of `prooph/php-cs-fixer-config`.
45
* (c) 2016-2018 prooph software GmbH <contact@prooph.de>
56
* (c) 2016-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
67
*
@@ -50,11 +51,10 @@ public function it_has_rules(): void
5051
/**
5152
* @test
5253
*/
53-
public function it_does_not_have_header_comment_fixer_by_default(): void
54+
public function it_does_have_header_comment_fixer_by_default(): void
5455
{
5556
$config = new Prooph();
5657
$rules = $config->getRules();
5758
$this->assertArrayHasKey('header_comment', $rules);
58-
$this->assertFalse($rules['header_comment']);
5959
}
6060
}

0 commit comments

Comments
 (0)