Skip to content

Commit

Permalink
✨ Implement operator whitespace fixer
Browse files Browse the repository at this point in the history
And basic stuff, which is needed in every fixer.
  • Loading branch information
DrWh0286 committed Nov 28, 2019
1 parent e0d7909 commit 64ed2ef
Show file tree
Hide file tree
Showing 40 changed files with 7,613 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.Build
composer.lock
composer.lock
.phpunit.result.cache
coverage
4 changes: 4 additions & 0 deletions bin/autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
declare(strict_types=1);

require __DIR__ . '/../.Build/vendor/autoload.php';
19 changes: 19 additions & 0 deletions bin/tscsf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env php
<?php

if (is_file(__DIR__ . '/../autoload.php')) {
require __DIR__.'/../autoload.php';
} else {
require __DIR__ . '/autoload.php';
}

use Pluswerk\TypoScriptAutoFixer\Command\FixCommand;
use Symfony\Component\Console\Application;

$application = new Application('test', '1.0.0');
$command = new FixCommand();

$application->add($command);

$application->setDefaultCommand('fix');
$application->run();
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
"grumphp": {
"config-default-path": "grumphp.yml"
}
}
},
"bin": "./bin/tscsf"
}
30 changes: 30 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.4/phpunit.xsd"
bootstrap=".Build/vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true">
<extensions>
<extension class="Pluswerk\TypoScriptAutoFixer\Hook\BypassFinalHook"/>
</extensions>
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>

<logging>
<log type="coverage-html" target="coverage/html"/>
<log type="coverage-clover" target="coverage/clover.xml"/>
</logging>
</phpunit>
49 changes: 49 additions & 0 deletions src/Command/FixCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Command;

use Pluswerk\TypoScriptAutoFixer\Fixer\IssueFixer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class FixCommand extends Command
{
/**
* @var IssueFixer
*/
private $issueFixer;

public function __construct(string $name = null)
{
$name = $name ?? 'fix';
parent::__construct($name);
$this->issueFixer = new IssueFixer();
}

protected function configure()
{
$this->addArgument('files', InputArgument::IS_ARRAY, 'files to fix', []);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int|null
*/
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$files = $input->getArgument('files');
if (count($files) > 0) {
foreach ($files as $file) {
if (is_file($file)) {
$this->issueFixer->fixIssuesForFile($file);
}
}
}
return 0;
}
}
10 changes: 10 additions & 0 deletions src/Exception/FileNotWritableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Exception;

use RuntimeException;

final class FileNotWritableException extends RuntimeException
{
}
11 changes: 11 additions & 0 deletions src/Exception/InvalidIssueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Exception;

use Exception;

final class InvalidIssueException extends Exception
{

}
10 changes: 10 additions & 0 deletions src/Exception/NoOperatorFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Exception;

use RuntimeException;

final class NoOperatorFoundException extends RuntimeException
{
}
10 changes: 10 additions & 0 deletions src/Exception/WriteFileFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Exception;

use RuntimeException;

final class WriteFileFailedException extends RuntimeException
{
}
97 changes: 97 additions & 0 deletions src/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer;

use Pluswerk\TypoScriptAutoFixer\Exception\FileNotWritableException;
use Pluswerk\TypoScriptAutoFixer\Exception\WriteFileFailedException;
use Pluswerk\TypoScriptAutoFixer\Issue\IssueCollection;

final class File extends \SplFileInfo
{
/**
* @var IssueCollection
*/
private $issues;

public function __construct($file_name)
{
parent::__construct($file_name);
$this->issues = new IssueCollection();
}

/**
* @param int $line
*
* @return string
*/
public function readLine(int $line): string
{
$fileObject = $this->openFile('r');
$fileObject->seek($line - 1);
return $fileObject->current();
}

/**
* @param string $lineValue
* @param int $line
*
* @todo: Try to make this method a little bit nicer ;-).
*/
public function replaceLine(string $lineValue, int $line): void
{
$fileObject = $this->openFile('r');
$content = '';
$fileObject->rewind();

while (!$fileObject->eof()) {
if ($fileObject->key() === ($line - 1)) {
$content .= $lineValue;

if (strpos($fileObject->current(), PHP_EOL) !== false) {
$content .= PHP_EOL;
}
} else {
$content .= $fileObject->current();
}

$fileObject->next();
}

$this->overwriteFileContent($content);
}

/**
* @param IssueCollection $issueCollection
*/
public function updateIssueCollection(IssueCollection $issueCollection): void
{
$this->issues = $issueCollection;
}

/**
* @param $content
*/
private function overwriteFileContent($content): void
{
try {
$writeFileObject = $this->openFile('w');
} catch (\RuntimeException $e) {
throw new FileNotWritableException($e->getMessage());
}

$writeResult = $writeFileObject->fwrite($content);

if ($writeResult === false || $writeResult === null) {
throw new WriteFileFailedException('write file ' . $this->getPathname() . ' failed!');
}
}

/**
* @return IssueCollection
*/
public function issues(): IssueCollection
{
return $this->issues;
}
}
38 changes: 38 additions & 0 deletions src/FileBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer;

final class FileBuilder
{
/**
* @var Linter
*/
private $linter;

/**
* FileBuilder constructor.
*
* @param Linter|null $linter
*/
public function __construct(Linter $linter = null)
{
$this->linter = $linter ?? new Linter();
}

/**
* @param string $filePath
*
* @return File
*/
public function buildFile(string $filePath): File
{
$file = new File($filePath);

$issueCollection = $this->linter->lint($filePath);

$file->updateIssueCollection($issueCollection);

return $file;
}
}
24 changes: 24 additions & 0 deletions src/Fixer/AbstractFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Fixer;

use Pluswerk\TypoScriptAutoFixer\FileBuilder;

abstract class AbstractFixer implements FixerInterface
{
/**
* @var FileBuilder
*/
protected $fileBuilder;

/**
* AbstractFixer constructor.
*
* @param FileBuilder $fileBuilder
*/
public function __construct(FileBuilder $fileBuilder = null)
{
$this->fileBuilder = $fileBuilder ?? new FileBuilder();
}
}
20 changes: 20 additions & 0 deletions src/Fixer/FixerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Fixer;

use Pluswerk\TypoScriptAutoFixer\Fixer\OperatorWhitespace\OperatorWhitespaceFixer;
use Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue;

final class FixerFactory
{
/**
* @param AbstractIssue $issue
*
* @return FixerInterface|null
*/
public function getFixerByIssue(AbstractIssue $issue): ?FixerInterface
{
return new OperatorWhitespaceFixer();
}
}
18 changes: 18 additions & 0 deletions src/Fixer/FixerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);

namespace Pluswerk\TypoScriptAutoFixer\Fixer;

use Pluswerk\TypoScriptAutoFixer\File;
use Pluswerk\TypoScriptAutoFixer\Issue\AbstractIssue;

interface FixerInterface
{
/**
* @param File $file
* @param AbstractIssue $issue
*
* @return File
*/
public function fixIssue(File $file, AbstractIssue $issue): File;
}
Loading

0 comments on commit 64ed2ef

Please sign in to comment.