Skip to content

Commit 4d23797

Browse files
committed
Implement traits
1 parent 33d06af commit 4d23797

File tree

8 files changed

+349
-0
lines changed

8 files changed

+349
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
use PhpCsFixer\Config;
6+
use PhpCsFixer\ToolInfo;
7+
use PhpCsFixer\ConfigInterface;
8+
use PhpCsFixer\ToolInfoInterface;
9+
use PhpCsFixer\Console\ConfigurationResolver;
10+
11+
trait HasConfigurationResolver
12+
{
13+
/**
14+
*
15+
* @var ConfigurationResolver
16+
*/
17+
protected $configResolver;
18+
19+
public function setResolver(array $options, ?ConfigInterface $config = null, ?string $cwd = null, ?ToolInfoInterface $toolInfo = null)
20+
{
21+
$config = $config ?: new Config(config('fixer.config_name', 'Laravel'));
22+
$cwd = $cwd ?: getcwd();
23+
$toolInfo = $toolInfo ?: new ToolInfo();
24+
$this->configResolver = new ConfigurationResolver($config, $options, $cwd, $toolInfo);
25+
}
26+
27+
public function getLoadedConfigMessage()
28+
{
29+
return sprintf(
30+
'Loaded config <comment>%s</comment> from %s.',
31+
$this->configResolver->getConfig()->getName(),
32+
$this->configResolver->getConfigFile() ?? 'Laravel Fixer Configuration.'
33+
);
34+
}
35+
36+
public function getPhpRuntimeMessage()
37+
{
38+
$configFile = $this->configResolver->getConfig()->getPhpExecutable();
39+
return sprintf('Runtime: <info>PHP %s</info>', PHP_VERSION);
40+
}
41+
42+
public function isUsingCache()
43+
{
44+
return $this->configResolver->getUsingCache();
45+
}
46+
47+
public function cacheFileExists()
48+
{
49+
return is_file($this->configResolver->getCacheFile());
50+
}
51+
52+
public function getCacheFileMessage()
53+
{
54+
return sprintf('Using cache file "%s".', $this->configResolver->getCacheFile());
55+
}
56+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
use PhpCsFixer\Console\Output\ErrorOutput;
6+
use PhpCsFixer\Error\ErrorsManager;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
trait HasErrorsManager
10+
{
11+
12+
/**
13+
*
14+
* @var ErrorsManager
15+
*/
16+
protected $errorsManager;
17+
18+
/**
19+
*
20+
* @var ErrorOutput
21+
*/
22+
protected $errorOutput;
23+
24+
public function setErrorsManager(?ErrorsManager $errorsManager = null)
25+
{
26+
$this->errorsManager = $errorsManager ?? new ErrorsManager();
27+
}
28+
29+
public function getErrorsManager()
30+
{
31+
if ($this->errorsManager === null) {
32+
$this->setErrorsManager();
33+
}
34+
35+
return $this->errorsManager;
36+
}
37+
38+
public function getErrorOutput(?OutputInterface $output = null)
39+
{
40+
if ($this->errorOutput === null) {
41+
$this->errorOutput = new ErrorOutput($output);
42+
}
43+
return $this->errorOutput;
44+
}
45+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
6+
use Symfony\Component\EventDispatcher\EventDispatcher;
7+
8+
9+
trait HasEventDispatcher
10+
{
11+
12+
/**
13+
*
14+
* @var EventDispatcher
15+
*/
16+
protected $eventDispatcher;
17+
18+
public function setEventDispatcher(?EventDispatcher $eventDispatcher = null)
19+
{
20+
$this->eventDispatcher = $eventDispatcher ?? new EventDispatcher();
21+
}
22+
23+
public function getEventDispatcher()
24+
{
25+
if ($this->eventDispatcher === null) {
26+
$this->setEventDispatcher();
27+
}
28+
return $this->eventDispatcher;
29+
}
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
use PhpCsFixer\Finder;
6+
7+
trait HasFinder
8+
{
9+
10+
/**
11+
*
12+
* @var Finder
13+
*/
14+
private $finder;
15+
16+
public function setFinder()
17+
{
18+
$this->finder = Finder::create()
19+
->in(config('fixer.find_directories'))
20+
->name(config('fixer.file_name_pattern_whitelist'))
21+
->notName(config('fixer.file_name_pattern_blacklist'))
22+
->ignoreDotFiles(config('fixer.ignore_dot_files'))
23+
->ignoreVCS(config('fixer.ignore_vcs'));
24+
}
25+
26+
public function getFinder()
27+
{
28+
if ($this->finder === null) {
29+
$this->setFinder();
30+
}
31+
32+
return $this->finder;
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
use PhpCsFixer\Console\Report\FixReport\ReportSummary;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
trait HasFixEvent
9+
{
10+
11+
use HasConfigurationResolver;
12+
use HasStopWatch;
13+
use HasRunner;
14+
use HasErrorsManager;
15+
16+
public function report(int $verbosity, OutputInterface $output)
17+
{
18+
$reportSummary = new ReportSummary(
19+
$this->filesModified,
20+
$this->getStopWatch()->getEvent('fixFiles')->getDuration(),
21+
$this->getStopWatch()->getEvent('fixFiles')->getMemory(),
22+
OutputInterface::VERBOSITY_VERBOSE <= $verbosity,
23+
$this->configResolver->isDryRun(),
24+
$output->isDecorated()
25+
);
26+
27+
28+
$output->isDecorated()
29+
? $output->write($this->configResolver->getReporter()->generate($reportSummary))
30+
: $output->write($this->configResolver->getReporter()->generate($reportSummary), false, OutputInterface::OUTPUT_RAW);;
31+
32+
if (\count($this->getErrorsManager()->getInvalidErrors()) > 0) {
33+
$this->getErrorOutput($output)->listErrors('linting before fixing', $this->getErrorsManager()->getInvalidErrors());
34+
}
35+
36+
if (\count($this->getErrorsManager()->getExceptionErrors()) > 0) {
37+
$this->getErrorOutput($output)->listErrors('fixing', $this->getErrorsManager()->getExceptionErrors());
38+
}
39+
40+
if (\count($this->getErrorsManager()->getLintErrors()) > 0) {
41+
$this->getErrorOutput($output)->listErrors('linting after fixing', $this->getErrorsManager()->getLintErrors());
42+
}
43+
}
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
use Symfony\Component\Console\Terminal;
6+
use PhpCsFixer\Console\Output\NullOutput;
7+
use PhpCsFixer\Console\Output\ProcessOutput;
8+
use Symfony\Component\Console\Output\OutputInterface;
9+
10+
trait HasProgressOutput
11+
{
12+
use HasEventDispatcher;
13+
14+
/**
15+
*
16+
* @var OutputInterface
17+
*/
18+
private $output;
19+
20+
/**
21+
*
22+
* @var ProcessOutput|NullOutput
23+
*/
24+
private $progressOutput;
25+
26+
/**
27+
* @param $resolver
28+
* @param $stdErr
29+
*
30+
*/
31+
protected function setProgressOutput()
32+
{
33+
$this->progressOutput = new NullOutput();
34+
35+
if ('none' !== $this->configResolver->getProgress()) {
36+
$this->progressOutput = new ProcessOutput(
37+
$this->output,
38+
$this->eventDispatcher,
39+
(new Terminal())->getWidth(),
40+
$this->finder->count()
41+
);
42+
}
43+
}
44+
45+
public function getProgressOutput()
46+
{
47+
return $this->getProgressOutput();
48+
}
49+
50+
public function printLegend()
51+
{
52+
$this->progressOutput->printLegend();
53+
}
54+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
use PhpCsFixer\Runner\Runner;
6+
7+
trait HasRunner
8+
{
9+
use HasFinder;
10+
use HasStopWatch;
11+
use HasErrorsManager;
12+
use HasEventDispatcher;
13+
use HasConfigurationResolver;
14+
15+
/**
16+
*
17+
* @var Runner
18+
*/
19+
protected $runner;
20+
21+
/**
22+
*
23+
* @var array
24+
*/
25+
protected $filesModified;
26+
27+
public function setRunner(?Runner $runner = null)
28+
{
29+
$this->runner = new Runner(
30+
$this->getFinder(),
31+
$this->configResolver->getFixers(),
32+
$this->configResolver->getDiffer(),
33+
'none' !== $this->configResolver->getProgress() ? $this->getEventDispatcher() : null,
34+
$this->getErrorsManager(),
35+
$this->configResolver->getLinter(),
36+
$this->configResolver->isDryRun(),
37+
$this->configResolver->getCacheManager(),
38+
$this->configResolver->getDirectory(),
39+
$this->configResolver->shouldStopOnViolation()
40+
);
41+
}
42+
43+
public function getRunner()
44+
{
45+
if ($this->runner === null) {
46+
$this->setRunner();
47+
}
48+
return $this->runner;
49+
}
50+
51+
public function fixFiles()
52+
{
53+
$this->getStopWatch()->start('fixFiles');
54+
$this->filesModified = $this->getRunner()->fix();
55+
$this->getStopWatch()->stop('fixFiles');
56+
}
57+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace STS\Fixer\Services\Fixer\Concerns;
4+
5+
use Symfony\Component\Stopwatch\Stopwatch;
6+
7+
trait HasStopWatch
8+
{
9+
10+
/**
11+
*
12+
* @var Stopwatch
13+
*/
14+
private $stopWatch;
15+
16+
public function setStopWatch()
17+
{
18+
$this->stopWatch = new Stopwatch();
19+
}
20+
21+
public function getStopWatch()
22+
{
23+
if ($this->stopWatch === null) {
24+
$this->setStopWatch();
25+
}
26+
27+
return $this->stopWatch;
28+
}
29+
}

0 commit comments

Comments
 (0)