From d9bfb427d8d676b8707d74bc75efb25261435efc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Wed, 11 Nov 2015 00:15:38 +0100 Subject: [PATCH] Changes for PHP7 compatibility --- .idea/encodings.xml | 5 ---- .idea/misc.xml | 5 ---- .idea/modules.xml | 9 ------- .idea/myapp.iml | 9 ------- .idea/vcs.xml | 7 ----- src/BackslashFixer/Fixer/FileEditor.php | 35 ++++++++++++++++++------- 6 files changed, 25 insertions(+), 45 deletions(-) delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/myapp.iml delete mode 100644 .idea/vcs.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index e206d70..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 1162f43..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index ecd5807..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/.idea/myapp.iml b/.idea/myapp.iml deleted file mode 100644 index 6b8184f..0000000 --- a/.idea/myapp.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index c80f219..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - diff --git a/src/BackslashFixer/Fixer/FileEditor.php b/src/BackslashFixer/Fixer/FileEditor.php index bfd1a7a..a59d47f 100644 --- a/src/BackslashFixer/Fixer/FileEditor.php +++ b/src/BackslashFixer/Fixer/FileEditor.php @@ -14,15 +14,21 @@ class FileEditor { - const BACKSLASH_TOKEN = 388; - const FUNCTION_TOKEN = 310; - /** * @var array */ private static $constants = []; + /** + * @var \Zend\Code\Generator\FileGenerator + */ private $fileGenerator; + /** + * @var FunctionRepository + */ private $functions; + /** + * @var Interfaces\FileSystem + */ private $fileSystem; /** @@ -61,26 +67,33 @@ public function addBackslashes($path) $token = [0 => 0, 1 => $tempToken, 2 => 0]; } - if ($token[0] == self::FUNCTION_TOKEN) { + if ($token[0] == T_STRING) { $reservedToken = $token[1]; //isFunction if (!empty($functions[$reservedToken]) - && $previousToken[0] != self::BACKSLASH_TOKEN - && $previousToken[0] != T_LIST + && $previousToken[0] != T_NAMESPACE + && $previousToken[0] != T_OBJECT_OPERATOR ) { $line = $token[2]; $source[$line-1] = str_replace($reservedToken, '\\'.$reservedToken, $source[$line-1]); } //isConstant - if (!empty($constants[strtoupper($reservedToken)]) - && $previousToken[0] != self::BACKSLASH_TOKEN + if (!empty($constants[strtoupper($token[1])]) + && $previousToken[0] != T_NAMESPACE + && !in_array(strtolower($token[1]), ['true', 'false', 'null']) ) { $line = $token[2]; - $source[$line-1] = str_replace($reservedToken, '\\'.$reservedToken, $source[$line-1]); + $source[$line-1] = str_replace($token[1], '\\'.$token[1], $source[$line-1]); } } + + if (in_array(strtolower($token[1]), ['true', 'false', 'null'])) { + $line = $token[2]; + $source[$line-1] = str_replace($token[1], '\\'.$token[1], $source[$line-1]); + } + $previousToken = $token; } @@ -129,7 +142,9 @@ private function getDefinedConstants() $c = array_values(self::$constants); - return array_combine($c, $c); + self::$constants = array_combine($c, $c); + + return self::$constants; } /**