Skip to content
Permalink
Browse files

PhpdocSelfAccessorFixer - handle "\E" in FQCN (#174)

  • Loading branch information...
kubawerlos committed Nov 7, 2019
1 parent fc8dcd8 commit 2443b83696b0bb0435a1aa75ed0185515400411d
@@ -8,7 +8,7 @@

[![Build status](https://img.shields.io/travis/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://travis-ci.org/kubawerlos/php-cs-fixer-custom-fixers)
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
![Tests](https://img.shields.io/badge/tests-1359-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-1360-brightgreen.svg)
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)

@@ -75,22 +75,22 @@ private function fixPhpdocSelfAccessor(Tokens $tokens, int $namespaceStartIndex,
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
$name = $tokens[$nameIndex]->getContent();
$classyName = $tokens[$nameIndex]->getContent();
$this->replaceNameOccurrences($tokens, $fullName, $name, $startIndex, $endIndex);
$this->replaceNameOccurrences($tokens, $fullName, $classyName, $startIndex, $endIndex);
$index = $endIndex;
}
}
private function replaceNameOccurrences(Tokens $tokens, string $namespace, string $name, int $startIndex, int $endIndex): void
private function replaceNameOccurrences(Tokens $tokens, string $namespace, string $classyName, int $startIndex, int $endIndex): void
{
for ($index = $startIndex; $index < $endIndex; $index++) {
if (!$tokens[$index]->isGivenKind(T_DOC_COMMENT)) {
continue;
}
$newContent = $this->getNewContent($tokens[$index]->getContent(), $namespace, $name);
$newContent = $this->getNewContent($tokens[$index]->getContent(), $namespace, $classyName);
if ($newContent === $tokens[$index]->getContent()) {
continue;
@@ -99,11 +99,11 @@ private function replaceNameOccurrences(Tokens $tokens, string $namespace, strin
}
}
private function getNewContent(string $content, string $namespace, string $name): string
private function getNewContent(string $content, string $namespace, string $classyName): string
{
$docBlock = new DocBlock($content);
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $name;
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $classyName;
foreach ($docBlock->getAnnotations() as $annotation) {
if (!$annotation->supportTypes()) {
@@ -114,7 +114,7 @@ private function getNewContent(string $content, string $namespace, string $name)
foreach ($annotation->getTypes() as $type) {
/** @var string $type */
$type = Preg::replace(
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|\Q%s\E)\b(?!\\\\)/', $name, $fqcn),
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|%s)\b(?!\\\\)/', $classyName, \preg_quote($fqcn, '/')),
'self',
$type
);
@@ -115,7 +115,6 @@ static function (Token $token): string {
static::assertNotContains('preg_grep', $strings, $message);
static::assertNotContains('preg_match', $strings, $message);
static::assertNotContains('preg_match_all', $strings, $message);
static::assertNotContains('preg_quote', $strings, \sprintf('Class %s must not use preg_quote, it shall use \Q and \E escape sequences instead.', $className));
static::assertNotContains('preg_replace', $strings, $message);
static::assertNotContains('preg_replace_callback', $strings, $message);
static::assertNotContains('preg_split', $strings, $message);
@@ -201,5 +201,26 @@ public function init();
public function getInstance();
}',
];
yield [
'<?php
namespace Custom\Error;
class ReadingError {
/**
* @return self
*/
public static function create() {}
}
',
'<?php
namespace Custom\Error;
class ReadingError {
/**
* @return \Custom\Error\ReadingError
*/
public static function create() {}
}
',
];
}
}

0 comments on commit 2443b83

Please sign in to comment.
You can’t perform that action at this time.