Skip to content

Commit 2443b83

Browse files
authored
PhpdocSelfAccessorFixer - handle "\E" in FQCN (#174)
1 parent fc8dcd8 commit 2443b83

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![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)
1010
[![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)
11-
![Tests](https://img.shields.io/badge/tests-1359-brightgreen.svg)
11+
![Tests](https://img.shields.io/badge/tests-1360-brightgreen.svg)
1212
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
1313
[![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)
1414

src/Fixer/PhpdocSelfAccessorFixer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,22 @@ private function fixPhpdocSelfAccessor(Tokens $tokens, int $namespaceStartIndex,
7575

7676
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
7777

78-
$name = $tokens[$nameIndex]->getContent();
78+
$classyName = $tokens[$nameIndex]->getContent();
7979

80-
$this->replaceNameOccurrences($tokens, $fullName, $name, $startIndex, $endIndex);
80+
$this->replaceNameOccurrences($tokens, $fullName, $classyName, $startIndex, $endIndex);
8181

8282
$index = $endIndex;
8383
}
8484
}
8585

86-
private function replaceNameOccurrences(Tokens $tokens, string $namespace, string $name, int $startIndex, int $endIndex): void
86+
private function replaceNameOccurrences(Tokens $tokens, string $namespace, string $classyName, int $startIndex, int $endIndex): void
8787
{
8888
for ($index = $startIndex; $index < $endIndex; $index++) {
8989
if (!$tokens[$index]->isGivenKind(T_DOC_COMMENT)) {
9090
continue;
9191
}
9292

93-
$newContent = $this->getNewContent($tokens[$index]->getContent(), $namespace, $name);
93+
$newContent = $this->getNewContent($tokens[$index]->getContent(), $namespace, $classyName);
9494

9595
if ($newContent === $tokens[$index]->getContent()) {
9696
continue;
@@ -99,11 +99,11 @@ private function replaceNameOccurrences(Tokens $tokens, string $namespace, strin
9999
}
100100
}
101101

102-
private function getNewContent(string $content, string $namespace, string $name): string
102+
private function getNewContent(string $content, string $namespace, string $classyName): string
103103
{
104104
$docBlock = new DocBlock($content);
105105

106-
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $name;
106+
$fqcn = ($namespace !== '' ? '\\' . $namespace : '') . '\\' . $classyName;
107107

108108
foreach ($docBlock->getAnnotations() as $annotation) {
109109
if (!$annotation->supportTypes()) {
@@ -114,7 +114,7 @@ private function getNewContent(string $content, string $namespace, string $name)
114114
foreach ($annotation->getTypes() as $type) {
115115
/** @var string $type */
116116
$type = Preg::replace(
117-
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|\Q%s\E)\b(?!\\\\)/', $name, $fqcn),
117+
\sprintf('/(?<![a-zA-Z0-9_\x7f-\xff\\\\])(%s|%s)\b(?!\\\\)/', $classyName, \preg_quote($fqcn, '/')),
118118
'self',
119119
$type
120120
);

tests/AutoReview/SrcCodeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ static function (Token $token): string {
115115
static::assertNotContains('preg_grep', $strings, $message);
116116
static::assertNotContains('preg_match', $strings, $message);
117117
static::assertNotContains('preg_match_all', $strings, $message);
118-
static::assertNotContains('preg_quote', $strings, \sprintf('Class %s must not use preg_quote, it shall use \Q and \E escape sequences instead.', $className));
119118
static::assertNotContains('preg_replace', $strings, $message);
120119
static::assertNotContains('preg_replace_callback', $strings, $message);
121120
static::assertNotContains('preg_split', $strings, $message);

tests/Fixer/PhpdocSelfAccessorFixerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,26 @@ public function init();
201201
public function getInstance();
202202
}',
203203
];
204+
205+
yield [
206+
'<?php
207+
namespace Custom\Error;
208+
class ReadingError {
209+
/**
210+
* @return self
211+
*/
212+
public static function create() {}
213+
}
214+
',
215+
'<?php
216+
namespace Custom\Error;
217+
class ReadingError {
218+
/**
219+
* @return \Custom\Error\ReadingError
220+
*/
221+
public static function create() {}
222+
}
223+
',
224+
];
204225
}
205226
}

0 commit comments

Comments
 (0)