Skip to content

Commit

Permalink
Major refactor using the token_get_all for excellent accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil Portugués committed Nov 10, 2015
1 parent a761cc0 commit 195eb89
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 72 deletions.
3 changes: 2 additions & 1 deletion src/BackslashFixer/Command/FixerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function configure()
* @param InputInterface $input Input
* @param OutputInterface $output Output
*
* @return \int|\null|void
* @return \int|\\null|void
*
* @throws Exception
*/
Expand All @@ -58,6 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$output->write('Success!', \true);

return $output;
}
}
116 changes: 50 additions & 66 deletions src/BackslashFixer/Fixer/FileEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,32 @@
*/
namespace NilPortugues\BackslashFixer\Fixer;

use NilPortugues\BackslashFixer\Fixer\Interfaces\FileSystem;
use NilPortugues\BackslashFixer\Fixer\Interfaces\FileSystem as FileSystemInterface;
use Zend\Code\Generator\FileGenerator;

class FileEditor
{
/**
* @var array
*/
private static $characters = [" ", "(", ",", "!", "[", "="];
const BACKSLASH_TOKEN = 388;
const FUNCTION_TOKEN = 310;

/**
* @var array
*/
private static $constants = [];

private $fileGenerator;
private $functions;
private $fileSystem;

/**
* FileEditor constructor.
* @param FileGenerator $fileGenerator
* @param FunctionRepository $functionRepository
* @param FileSystem $fileSystem
* @param FileGenerator $fileGenerator
* @param FunctionRepository $functionRepository
* @param FileSystemInterface $fileSystem
*/
public function __construct(
FileGenerator $fileGenerator,
FunctionRepository $functionRepository,
FileSystem $fileSystem
FileSystemInterface $fileSystem
) {
$this->fileGenerator = $fileGenerator;
$this->functions = $functionRepository;
Expand All @@ -51,25 +48,50 @@ public function __construct(
public function addBackslashes($path)
{
$generator = $this->fileGenerator->fromReflectedFileName($path);
$source = $generator->getSourceContent();
$source = explode("\n", $generator->getSourceContent());
$tokens = token_get_all(file_get_contents($path));

$previousToken = null;
$functions = $this->getReplaceableFunctions($generator);
$constants = $this->getDefinedConstants();

foreach (self::$characters as $character) {
$functions = $this->buildFunctions($character);
$functions = $this->removeUseFunctionsFromBackslashing($generator, $functions);
$source = \str_replace($functions, $this->buildBackslashedFunctions($character), $source);
foreach ($tokens as $token) {
if (!is_array($token)) {
$tempToken = $token;
$token = [0 => 0, 1 => $tempToken, 2 => 0];
}

if ($token[0] == self::FUNCTION_TOKEN) {
$reservedToken = $token[1];

//isFunction
if (!empty($functions[$reservedToken])
&& $previousToken[0] != self::BACKSLASH_TOKEN
&& $previousToken[0] != T_LIST
) {
$line = $token[2];
$source[$line-1] = str_replace($reservedToken, '\\'.$reservedToken, $source[$line-1]);
}

//isConstant
if (!empty($constants[strtoupper($reservedToken)])
&& $previousToken[0] != self::BACKSLASH_TOKEN
) {
$line = $token[2];
$source[$line-1] = str_replace($reservedToken, '\\'.$reservedToken, $source[$line-1]);
}
}
$previousToken = $token;
}

$constants = $this->getDefinedConstants();
$source = \str_replace($constants, $this->replaceConstants($constants), $source);
$source = \str_replace(['true', 'false', 'null'], ['\true', '\false', '\null'], $source);
$source = implode("\n", $source);
$source = \str_replace("function \\", "function ", $source);
$source = \str_replace("const \\", "const ", $source);
$source = \str_replace("::\\", "::", $source);

$this->fileSystem->writeFile($path, $source);
}


/**
* If a method exists under a namespace and has been aliased, or has been imported, don't replace.
*
Expand All @@ -96,67 +118,29 @@ private function removeUseFunctionsFromBackslashing(FileGenerator $generator, ar
return $functions;
}



/**
* @param string $previousCharacter
* @return array
*/
private function buildBackslashedFunctions($previousCharacter = ' ')
private function getDefinedConstants()
{
$backSlashedFunctions = $this->functions->getFunctions();
if (empty(self::$constants)) {
self::$constants = \array_keys(\get_defined_constants(\false));
}

$callback = function ($v) use ($previousCharacter) {
return $previousCharacter.'\\'.\ltrim($v, "\\")."(";
};
$c = array_values(self::$constants);

return \array_map($callback, $backSlashedFunctions);
return array_combine($c, $c);
}

/**
* @param string $previousCharacter
* @param $generator
* @return array
*/
private function buildFunctions($previousCharacter = ' ')
protected function getReplaceableFunctions($generator)
{
$functions = $this->functions->getFunctions();
$callback = function ($v) use ($previousCharacter) {
return $previousCharacter.$v."(";
};
$functions = \array_map($callback, $functions);


$functions = $this->removeUseFunctionsFromBackslashing($generator, $functions);

return $functions;
}

/**
* @param array $constants
*
* @return array
*/
private function replaceConstants(array $constants)
{

$callback = function ($v) {
return sprintf('\%s', $v);
};

$a = \array_map($callback, $constants);

return $a;
}

/**
* @return array
*/
private function getDefinedConstants()
{
if (empty(self::$constants)) {
self::$constants = \array_keys(\get_defined_constants(\false));
}

$c = array_values(self::$constants);
return array_combine($c, $c);
}
}
3 changes: 1 addition & 2 deletions src/BackslashFixer/Fixer/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace NilPortugues\BackslashFixer\Fixer;


use InvalidArgumentException;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
Expand Down Expand Up @@ -43,4 +42,4 @@ public function writeFile($filePath, $fileContent)
{
\file_put_contents($filePath, $fileContent);
}
}
}
5 changes: 2 additions & 3 deletions src/BackslashFixer/Fixer/Interfaces/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace NilPortugues\BackslashFixer\Fixer\Interfaces;


interface FileSystem
{
/**
* @param string $path
* @param string $path
* @return string[]
*/
public function getFilesFromPath($path);
Expand All @@ -16,4 +15,4 @@ public function getFilesFromPath($path);
* @param string $fileContent
*/
public function writeFile($filePath, $fileContent);
}
}

0 comments on commit 195eb89

Please sign in to comment.