|
@@ -12,16 +12,69 @@
|
|
|
namespace Symfony\CS\Fixer;
|
|
|
|
|
|
use Symfony\CS\FixerInterface;
|
|
|
+use Symfony\CS\Tokens;
|
|
|
|
|
|
/**
|
|
|
- * @author Fabien Potencier <fabien@symfony.com>
|
|
|
+ * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
|
|
*/
|
|
|
class ShortTagFixer implements FixerInterface
|
|
|
{
|
|
|
public function fix(\SplFileInfo $file, $content)
|
|
|
{
|
|
|
- // [Structure] Never use short tags (<?)
|
|
|
- return preg_replace('/<\?(\s|$)/', '<?php$1', $content);
|
|
|
+ // replace all <? with <?php to replace all short open tags even without short_open_tag option enabled
|
|
|
+ $newContent = preg_replace('/<\?(\s|$)/', '<?php$1', $content);
|
|
|
+
|
|
|
+ /* the following code is magic to revert previous replacements which should NOT be replaced, for example incorrectly replacing
|
|
|
+ * > echo '<? ';
|
|
|
+ * with
|
|
|
+ * > echo '<?php ';
|
|
|
+ */
|
|
|
+ $tokens = Tokens::fromCode($newContent);
|
|
|
+ $tokensOldContent = '';
|
|
|
+ $tokensOldContentLength = 0;
|
|
|
+
|
|
|
+ foreach ($tokens as $token) {
|
|
|
+ if ($token->isGivenKind(T_OPEN_TAG)) {
|
|
|
+ $tokenContent = $token->content;
|
|
|
+
|
|
|
+ if ('<?php' !== substr($content, $tokensOldContentLength, 5)) {
|
|
|
+ $tokenContent = '<? ';
|
|
|
+ }
|
|
|
+
|
|
|
+ $tokensOldContent .= $tokenContent;
|
|
|
+ $tokensOldContentLength += strlen($tokenContent);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($token->isGivenKind(array(T_COMMENT, T_DOC_COMMENT, T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE, T_STRING, ))) {
|
|
|
+ $tokenContent = '';
|
|
|
+ $tokenContentLength = 0;
|
|
|
+ $parts = explode('<?php ', $token->content);
|
|
|
+ $iLast = count($parts) - 1;
|
|
|
+
|
|
|
+ foreach ($parts as $i => $part) {
|
|
|
+ $tokenContent .= $part;
|
|
|
+ $tokenContentLength += strlen($part);
|
|
|
+
|
|
|
+ if ($i !== $iLast) {
|
|
|
+ if ('<?php' === substr($content, $tokensOldContentLength + $tokenContentLength, 5)) {
|
|
|
+ $tokenContent .= '<?php ';
|
|
|
+ $tokenContentLength += 5;
|
|
|
+ } else {
|
|
|
+ $tokenContent .= '<? ';
|
|
|
+ $tokenContentLength += 3;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $token->content = $tokenContent;
|
|
|
+ }
|
|
|
+
|
|
|
+ $tokensOldContent .= $token->content;
|
|
|
+ $tokensOldContentLength += strlen($token->content);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $tokens->generateCode();
|
|
|
}
|
|
|
|
|
|
public function getLevel()
|