Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit 7a3d231

Browse files
committed
Merge pull request #3 from mmoreram/feature/php-header-command
New feature: PHP Header fixer
2 parents 7b989e3 + 6296d8d commit 7a3d231

File tree

13 files changed

+556
-25
lines changed

13 files changed

+556
-25
lines changed

.formatter.yml

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,18 @@ use-sort:
44
- Mmoreram
55
group-type: each
66
sort-type: alph
7-
sort-direction: asc
7+
sort-direction: asc
8+
9+
header: |
10+
/**
11+
* This file is part of the php-formatter package
12+
*
13+
* Copyright (c) 2014 Marc Morera
14+
*
15+
* For the full copyright and license information, please view the LICENSE
16+
* file that was distributed with this source code.
17+
*
18+
* Feel free to edit as you please, and have fun.
19+
*
20+
* @author Marc Morera <[email protected]>
21+
*/

README.md

+76-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ PHP Formatter
55
[![Latest Stable Version](https://poser.pugx.org/mmoreram/php-formatter/v/stable.png)](https://packagist.org/packages/mmoreram/php-formatter)
66
[![Latest Unstable Version](https://poser.pugx.org/mmoreram/php-formatter/v/unstable.png)](https://packagist.org/packages/mmoreram/php-formatter)
77

8+
This PHP formatter aims to provide you some bulk actions for you PHP projects to
9+
ensure their consistency. Any of them fixes PSR rules. If you want to fix PSR
10+
rules, please check [fabpot/php-cs-fixer](https://github.com/fabpot/PHP-CS-Fixer).
11+
12+
## Tags
13+
14+
* Use last unstable version ( alias of `dev-master` ) to stay in last commit
15+
* Use last stable version tag to stay in a stable release.
16+
* [![Latest Unstable Version](https://poser.pugx.org/mmoreram/php-formatter/v/unstable.png)](https://packagist.org/packages/mmoreram/php-formatter)
17+
[![Latest Stable Version](https://poser.pugx.org/mmoreram/php-formatter/v/stable.png)](https://packagist.org/packages/mmoreram/php-formatter)
18+
819
## Install
920

1021
Install PHP Formatter in this way:
@@ -76,13 +87,27 @@ use-sort:
7687
group-type: each
7788
sort-type: alph
7889
sort-direction: asc
90+
91+
header: |
92+
/**
93+
* This file is part of the php-formatter package
94+
*
95+
* Copyright (c) 2014 Marc Morera
96+
*
97+
* For the full copyright and license information, please view the LICENSE
98+
* file that was distributed with this source code.
99+
*
100+
* Feel free to edit as you please, and have fun.
101+
*
102+
* @author Marc Morera <[email protected]>
103+
*/
79104
```
80105
81106
you can also define where to search the `.formatter.yml` file using the
82107
`--config|-c` option
83108

84109
``` bash
85-
$ php-formatter use:sort src/ --config="src/"
110+
$ php-formatter formatter:use:sort src/ --config="src/"
86111
```
87112

88113
## Commands
@@ -107,10 +132,11 @@ Options:
107132
--no-interaction -n Do not ask any interactive question.
108133
109134
Available commands:
110-
help Displays help for a command
111-
list Lists commands
112-
use
113-
use:sort Sort Use statements
135+
help Displays help for a command
136+
list Lists commands
137+
formatter
138+
formatter:header:fix Ensures that all PHP files has header defined in config file
139+
formatter:use:sort Sort Use statements
114140
```
115141

116142
### Sort all Use Statements
@@ -119,7 +145,7 @@ You can sort your Use Statements in a very different ways. For this command you
119145
must provide as an argument the path where to look for the PHP files you want to
120146
process.
121147

122-
* command: `php-formatter use:sort`
148+
* command: `php-formatter formatter:use:sort`
123149
* argument: path
124150
* option: --group [***multiple***]
125151
* option: --group-type=***one***|***each***
@@ -133,7 +159,7 @@ You can use this tool just to test the files will be modified, using option
133159
--dry-run
134160

135161
``` bash
136-
$ php-formatter use:sort src/ --dry-run
162+
$ php-formatter formatter:use:sort src/ --dry-run
137163
```
138164

139165
#### Group
@@ -146,7 +172,7 @@ Common group is named `_main` and if is not specified, is placed in the
146172
begining. You can define where to place this group with `--group` option
147173

148174
``` bash
149-
$ php-formatter use:sort src/ --group="Mmoreram" --group="_main" --group="Symfony"
175+
$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group="_main" --group="Symfony"
150176
```
151177

152178
This command will sort the code like this
@@ -169,7 +195,7 @@ Finally, the `--group-type` defines if you want one `use` literal in every
169195
namespace line
170196

171197
``` bash
172-
$ php-formatter use:sort src/ --group="Mmoreram" --group-type="each"
198+
$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group-type="each"
173199
```
174200

175201
This command will sort the code like this
@@ -187,7 +213,7 @@ use Mmoreram\MySecondClass;
187213
or if you want only one use for all group.
188214

189215
``` bash
190-
$ php-formatter use:sort src/ --group="Mmoreram" --group-type="one"
216+
$ php-formatter formatter:use:sort src/ --group="Mmoreram" --group-type="one"
191217
```
192218

193219
This command will sort the code like this
@@ -208,7 +234,7 @@ There are two options of sorting. You can sort your namespaces alphabetically
208234
***(default value)***
209235

210236
``` bash
211-
$ php-formatter use:sort src/ --sort-type="alph"
237+
$ php-formatter formatter:use:sort src/ --sort-type="alph"
212238
```
213239

214240
This command will sort the code like this
@@ -225,7 +251,7 @@ use Symfony\OneClass;
225251
or by length (two namespaces with same length will be sorted alphabetically)
226252

227253
``` bash
228-
$ php-formatter use:sort src/ --sort-type="length"
254+
$ php-formatter formatter:use:sort src/ --sort-type="length"
229255
```
230256

231257
This command will sort the code like this
@@ -243,7 +269,7 @@ You can also define the direction of the sorting. This can be ascending
243269
***(default value)***
244270

245271
``` bash
246-
$ php-formatter use:sort src/ --sort-direction="asc"
272+
$ php-formatter formatter:use:sort src/ --sort-direction="asc"
247273
```
248274

249275
This command will sort the code like this
@@ -260,7 +286,7 @@ use Symfony\OneClass;
260286
or descending
261287

262288
``` bash
263-
$ php-formatter use:sort src/ --sort-direction="desc"
289+
$ php-formatter formatter:use:sort src/ --sort-direction="desc"
264290
```
265291

266292
This command will sort the code like this
@@ -273,3 +299,39 @@ use Mmoreram\MySecondClass;
273299
use Mmoreram\MyClass;
274300
use AnotherBundle\AnotherClass;
275301
```
302+
303+
### Fix all PHP headers
304+
305+
You can define your PHP header in your `.formatter.yml` file an this command
306+
will check and fix that all PHP files have it properly.
307+
308+
* command: `php-formatter formatter:header:fix`
309+
* argument: path
310+
* option: --dry-run [***no value***]
311+
312+
#### Dry run
313+
314+
You can use this tool just to test the files will be modified, using option
315+
--dry-run
316+
317+
``` bash
318+
$ php-formatter formatter:header:fix src/ --dry-run
319+
```
320+
321+
Your PHP Header definition must have this format.
322+
323+
``` yml
324+
header: |
325+
/**
326+
* This file is part of the php-formatter package
327+
*
328+
* Copyright (c) 2014 Marc Morera
329+
*
330+
* For the full copyright and license information, please view the LICENSE
331+
* file that was distributed with this source code.
332+
*
333+
* Feel free to edit as you please, and have fun.
334+
*
335+
* @author Marc Morera <[email protected]>
336+
*/
337+
```

bin/compile

100644100755
File mode changed.

build/php-formatter.phar

100755100644
-97.6 KB
Binary file not shown.
+171
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the php-formatter package
5+
*
6+
* Copyright (c) 2014 Marc Morera
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* Feel free to edit as you please, and have fun.
12+
*
13+
* @author Marc Morera <[email protected]>
14+
*/
15+
16+
namespace Mmoreram\PHPFormatter\Command;
17+
18+
use Exception;
19+
use Symfony\Component\Console\Command\Command;
20+
use Symfony\Component\Console\Input\InputArgument;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Input\InputOption;
23+
use Symfony\Component\Console\Output\OutputInterface;
24+
use Symfony\Component\Filesystem\Filesystem;
25+
26+
use Mmoreram\PHPFormatter\Finder\ConfigFinder;
27+
use Mmoreram\PHPFormatter\Finder\FileFinder;
28+
use Mmoreram\PHPFormatter\Fixer\HeaderFixer;
29+
use Mmoreram\PHPFormatter\Loader\ConfigLoader;
30+
31+
/**
32+
* Class HeaderCommand
33+
*/
34+
class HeaderCommand extends Command
35+
{
36+
/**
37+
* @var string
38+
*
39+
* Command name
40+
*/
41+
const COMMAND_NAME = 'header';
42+
43+
/**
44+
* configure
45+
*/
46+
protected function configure()
47+
{
48+
$this
49+
->setName('formatter:header:fix')
50+
->setDescription('Ensures that all PHP files has header defined in config file')
51+
->addArgument(
52+
'path',
53+
InputArgument::REQUIRED,
54+
'Path'
55+
)
56+
->addOption(
57+
'--config',
58+
'-c',
59+
InputOption::VALUE_OPTIONAL,
60+
"Config file directory",
61+
getcwd()
62+
)
63+
->addOption(
64+
'dry-run',
65+
null,
66+
InputOption::VALUE_NONE,
67+
"Just print the result, nothing is overwritten"
68+
);
69+
}
70+
71+
/**
72+
* Execute command
73+
*
74+
* @param InputInterface $input Input
75+
* @param OutputInterface $output Output
76+
*
77+
* @return int|null|void
78+
*
79+
* @throws Exception
80+
*/
81+
protected function execute(InputInterface $input, OutputInterface $output)
82+
{
83+
$verbose = $output->getVerbosity();
84+
$path = $input->getArgument('path');
85+
$dryRun = $input->getOption('dry-run');
86+
$fileFinder = new FileFinder;
87+
$configLoader = new ConfigLoader;
88+
$configFinder = new ConfigFinder;
89+
90+
/**
91+
* This section is just for finding the right values to work with in
92+
* this execution.
93+
*
94+
* $options array will have, after this block, all these values
95+
*/
96+
$configPath = rtrim($input->getOption('config'), DIRECTORY_SEPARATOR);
97+
98+
$header = $configLoader->loadConfigValue(
99+
self::COMMAND_NAME,
100+
$configFinder->findConfigFile($configPath)
101+
);
102+
103+
/**
104+
* Building the real directory or file to work in
105+
*/
106+
$filesystem = new Filesystem();
107+
if (!$filesystem->isAbsolutePath($path)) {
108+
$path = getcwd() . DIRECTORY_SEPARATOR . $path;
109+
}
110+
111+
if (!is_file($path) && !is_dir($path)) {
112+
113+
throw new Exception('Directory or file "' . $path . '" does not exist');
114+
}
115+
116+
/**
117+
* Dry-run message
118+
*/
119+
if ($dryRun && $verbose >= OutputInterface::VERBOSITY_VERBOSE) {
120+
121+
$output->writeln('# This process has been executed in mode dry-run');
122+
}
123+
124+
if ($verbose >= OutputInterface::VERBOSITY_VERBOSE) {
125+
126+
$output->writeln('# Executing process in ' . $path);
127+
}
128+
129+
/**
130+
* Creates the new HeaderFixer
131+
*/
132+
$headerFixer = new HeaderFixer($header);
133+
134+
$files = $fileFinder->findPHPFilesByPath($path);
135+
136+
/**
137+
* If verbose level is higher or equal than -vv, we print the config
138+
* file data, if is not empty.
139+
*/
140+
if ($verbose >= OutputInterface::VERBOSITY_VERBOSE) {
141+
142+
$output->writeln("# Header used:\n\n" . $header);
143+
}
144+
145+
$output->writeln('#');
146+
147+
/**
148+
* Each found php file is processed
149+
*/
150+
foreach ($files as $file) {
151+
152+
$data = file_get_contents($file);
153+
$result = $headerFixer->fix($data);
154+
155+
if ($result === false || $data === $result) {
156+
157+
continue;
158+
}
159+
160+
if ($verbose >= OutputInterface::VERBOSITY_NORMAL) {
161+
162+
$output->writeln('# ' . $file);
163+
}
164+
165+
if (!$dryRun) {
166+
167+
file_put_contents($file, $result);
168+
}
169+
}
170+
}
171+
}

src/PHPFormatter/Command/UseSortCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class UseSortCommand extends Command
4747
protected function configure()
4848
{
4949
$this
50-
->setName('use:sort')
50+
->setName('formatter:use:sort')
5151
->setDescription('Sort Use statements')
5252
->addArgument(
5353
'path',

src/PHPFormatter/Console/Application.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected function getDefaultCommands()
4848
{
4949
$commands = parent::getDefaultCommands();
5050
$commands[] = new Command\UseSortCommand();
51+
$commands[] = new Command\HeaderCommand();
5152

5253
return $commands;
5354
}

0 commit comments

Comments
 (0)