Skip to content

Commit 6fb92f6

Browse files
author
Tomáš Votruba
authored
Merge pull request #8 from Symplify/fixing-command
Fixing command
2 parents e3fefc3 + 690db6f commit 6fb92f6

18 files changed

+281
-50
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@
3030
"phpunit/phpunit": "^5.3",
3131
"nette/utils": "^2.3",
3232
"symplify/coding-standard": "^0.2.0"
33-
}
33+
},
34+
"bin": ["bin/multi-cs"]
3435
}

multi-cs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"PSR2"
44
],
55
"sniffs": [
6-
"SymplifyCodingStandard\\Sniffs\\Naming\\AbstractClassNameSniff"
6+
"SymplifyCodingStandard.Naming.AbstractClassName"
77
],
88
"exclude-sniffs": [
9-
"SymplifyCodingStandard\\Sniffs\\Naming\\AbstractClassNameSniff"
9+
"SymplifyCodingStandard.Naming.AbstractClassName"
1010
],
1111
"fixer-levels": [
1212
"psr1",

source/SomeAbstractClass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
abstract class SomeClass
4+
{
5+
6+
}

src/CodeSniffer/CodeBeautifier.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Symplify
5+
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6+
*/
7+
8+
namespace Symplify\MultiCodingStandard\CodeSniffer;
9+
10+
use PHP_CodeSniffer;
11+
12+
final class CodeBeautifier extends PHP_CodeSniffer
13+
{
14+
}
15+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Symplify
5+
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6+
*/
7+
8+
namespace Symplify\MultiCodingStandard\CodeSniffer;
9+
10+
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeBeautifierFactoryInterface;
11+
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeSnifferFactoryInterface;
12+
13+
final class CodeBeautifierFactory implements CodeBeautifierFactoryInterface
14+
{
15+
/**
16+
* @var CodeSnifferFactoryInterface
17+
*/
18+
private $codeSnifferFactory;
19+
20+
public function __construct(CodeSnifferFactoryInterface $codeSnifferFactory)
21+
{
22+
$this->codeSnifferFactory = $codeSnifferFactory;
23+
}
24+
25+
/**
26+
* @return CodeBeautifier
27+
*/
28+
public function create()
29+
{
30+
// high verbosity
31+
if (!defined('PHP_CODESNIFFER_VERBOSITY')) {
32+
define('PHP_CODESNIFFER_VERBOSITY', 1);
33+
}
34+
35+
// enables fixer
36+
if (!defined('PHP_CODESNIFFER_CBF')) {
37+
define('PHP_CODESNIFFER_CBF', true);
38+
}
39+
40+
return $this->codeSnifferFactory->create();
41+
}
42+
}

src/CodeSniffer/CodeSnifferFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(
5454
}
5555

5656
/**
57-
* @return PHP_CodeSniffer
57+
* {@inheritdoc}
5858
*/
5959
public function create()
6060
{

src/Command/CheckCommand.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Style\StyleInterface;
1717
use Symfony\Component\Finder\Finder;
1818
use Symplify\MultiCodingStandard\Console\ExitCode;
19+
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeSnifferFactoryInterface;
1920

2021
final class CheckCommand extends Command
2122
{
@@ -29,11 +30,18 @@ final class CheckCommand extends Command
2930
*/
3031
private $style;
3132

32-
public function __construct(PHP_CodeSniffer $codeSniffer, StyleInterface $style)
33-
{
33+
/**
34+
* @var CodeSnifferFactoryInterface
35+
*/
36+
private $codeSnifferFactory;
37+
38+
public function __construct(
39+
CodeSnifferFactoryInterface $codeSnifferFactory,
40+
StyleInterface $style
41+
) {
3442
parent::__construct();
3543

36-
$this->codeSniffer = $codeSniffer;
44+
$this->codeSnifferFactory = $codeSnifferFactory;
3745
$this->style = $style;
3846
}
3947

@@ -44,7 +52,7 @@ protected function configure()
4452
{
4553
$this->setName('check');
4654
$this->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s)', null);
47-
$this->setDescription('Check coding standard in particular directory');
55+
$this->setDescription('Check coding standard in one or more directories.');
4856
}
4957

5058
/**
@@ -72,7 +80,7 @@ private function checkDirectory($path)
7280
{
7381
// code sniffer
7482
foreach ((new Finder())->in($path)->files() as $filePath => $fileInfo) {
75-
$file = $this->codeSniffer->processFile($filePath);
83+
$file = $this->getCodeSniffer()->processFile($filePath);
7684

7785
}
7886

@@ -83,4 +91,21 @@ private function checkDirectory($path)
8391
sprintf('Directory "%s" was checked!', $path)
8492
);
8593
}
94+
95+
/**
96+
* Lazy loaded due to duplicated constants in setup
97+
* in CodeSniffer for both Sniffer and Fixer.
98+
*
99+
* @return PHP_CodeSniffer
100+
*/
101+
private function getCodeSniffer()
102+
{
103+
if ($this->codeSniffer) {
104+
return $this->codeSniffer;
105+
}
106+
107+
$this->codeSniffer = $this->codeSnifferFactory->create();
108+
109+
return $this->codeSniffer;
110+
}
86111
}

src/Command/FixCommand.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Symplify
5+
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6+
*/
7+
8+
namespace Symplify\MultiCodingStandard\Command;
9+
10+
use Exception;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Input\InputArgument;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\Console\Style\StyleInterface;
16+
use Symfony\Component\Finder\Finder;
17+
use Symfony\CS\Fixer;
18+
use Symplify\MultiCodingStandard\CodeSniffer\CodeBeautifier;
19+
use Symplify\MultiCodingStandard\CodeSniffer\CodeBeautifierFactory;
20+
use Symplify\MultiCodingStandard\Console\ExitCode;
21+
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeBeautifierFactoryInterface;
22+
23+
final class FixCommand extends Command
24+
{
25+
/**
26+
* @var CodeBeautifierFactoryInterface
27+
*/
28+
private $codeBeautifierFactory;
29+
30+
/**
31+
* @var StyleInterface
32+
*/
33+
private $style;
34+
35+
/**
36+
* @var Fixer
37+
*/
38+
private $fixer;
39+
40+
/**
41+
* @var CodeBeautifier
42+
*/
43+
private $codeBeautifier;
44+
45+
public function __construct(
46+
CodeBeautifierFactoryInterface $codeBeautifierFactory,
47+
Fixer $fixer,
48+
StyleInterface $style
49+
) {
50+
parent::__construct();
51+
52+
$this->codeBeautifierFactory = $codeBeautifierFactory;
53+
$this->fixer = $fixer;
54+
$this->style = $style;
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
protected function configure()
61+
{
62+
$this->setName('fix');
63+
$this->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s)', null);
64+
$this->setDescription('Fix coding standard in one or more directories.');
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
protected function execute(InputInterface $input, OutputInterface $output)
71+
{
72+
// note: needs to be lazy created, due to constant-options in PHP_CodeSniffer
73+
$this->codeBeautifier = $this->codeBeautifierFactory->create();
74+
75+
try {
76+
foreach ($input->getArgument('path') as $path) {
77+
$this->fixDirectory($path);
78+
}
79+
80+
return ExitCode::SUCCESS;
81+
} catch (Exception $exception) {
82+
$this->style->error($exception->getMessage());
83+
84+
return ExitCode::ERROR;
85+
}
86+
}
87+
88+
/**
89+
* @param string $path
90+
*/
91+
private function fixDirectory($path)
92+
{
93+
// code sniffer
94+
foreach (Finder::create()->in($path)->files() as $filePath => $fileInfo) {
95+
$file = $this->codeBeautifier->processFile($filePath);
96+
var_dump($file->getErrorCount());
97+
}
98+
99+
// php-cs-fixer
100+
101+
$this->style->success(
102+
sprintf('Directory "%s" was checked!', $path)
103+
);
104+
}
105+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Symplify
5+
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6+
*/
7+
8+
namespace Symplify\MultiCodingStandard\Contract\CodeSniffer;
9+
10+
use Symplify\MultiCodingStandard\CodeSniffer\CodeBeautifier;
11+
12+
interface CodeBeautifierFactoryInterface
13+
{
14+
/**
15+
* @return CodeBeautifier
16+
*/
17+
public function create();
18+
}

src/config/services.neon

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
1-
services:
2-
# PHP_CodeSniffer helpers
3-
- Symplify\MultiCodingStandard\CodeSniffer\CodeSnifferFactory
4-
-
5-
factory: [@Symplify\MultiCodingStandard\CodeSniffer\CodeSnifferFactory, "create"]
6-
- Symplify\MultiCodingStandard\CodeSniffer\FileSystem\SniffFileSystem(%appDir%/../vendor)
7-
- Symplify\MultiCodingStandard\CodeSniffer\FileSystem\RulesetFileSystem(%appDir%/../vendor)
8-
- Symplify\MultiCodingStandard\CodeSniffer\Naming\SniffNaming
9-
10-
# console
11-
- Symplify\MultiCodingStandard\Console\Application
12-
- Symplify\MultiCodingStandard\Command\CheckCommand
13-
- Symplify\MultiCodingStandard\Console\Style\StyleFactory
14-
-
15-
factory: [@Symplify\MultiCodingStandard\Console\Style\StyleFactory, "create"]
16-
- Symfony\Component\Console\Input\ArgvInput
17-
- Symfony\Component\Console\Output\ConsoleOutput
1+
includes:
2+
- services/console.neon
3+
- services/php-code-sniffer.neon
4+
- services/php-cs-fixer.neon
185

19-
# configuration
6+
services:
207
- Symplify\MultiCodingStandard\Configuration\Configuration
218
- Symplify\MultiCodingStandard\Configuration\MultiCsFileLoader(%configPath%)
22-
23-
# PHP-CS-Fixer
24-
- Symplify\MultiCodingStandard\PhpCsFixer\PhpCsFixerFactory
25-
-
26-
factory: [@Symplify\MultiCodingStandard\PhpCsFixer\PhpCsFixerFactory, "create"]
27-
- Symplify\MultiCodingStandard\PhpCsFixer\EnabledFixerResolver
28-
- Symplify\MultiCodingStandard\PhpCsFixer\FileSystem\FixerFileSystem
29-
- Symplify\MultiCodingStandard\PhpCsFixer\Fixer\FixerFactory
30-
- Symplify\MultiCodingStandard\PhpCsFixer\ConfigurationResolverFactory
31-
-
32-
factory: [@Symplify\MultiCodingStandard\PhpCsFixer\ConfigurationResolverFactory, "create"]

0 commit comments

Comments
 (0)