-
-
Notifications
You must be signed in to change notification settings - Fork 191
[CodingStandard] ClassStringToClassCosntantFixer init #262
Conversation
cd8ada1
to
db335eb
Compare
@dg I've implemented the |
@SpacePossum @keradus Could I ask you guys for short review? |
{ | ||
foreach (array_reverse($tokens->toArray(), true) as $index => $token) { | ||
/** @var Token $token */ | ||
if (! $this->isStringToken($token)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is possible directly look for code T_CONSTANT_ENCAPSED_STRING
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, that's the token I was looking for! Thank you
} | ||
|
||
$potentialClassOrInterfaceName = trim($token->getContent(), "'"); | ||
if (class_exists($potentialClassOrInterfaceName) || interface_exists($potentialClassOrInterfaceName)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class may not exist, because ECS is used for projects that uses own classes, so better is regexp checking. I am using this:
if (preg_match('#^[A-Z]\w*[a-z]\w*(\\\\[A-Z]\w*[a-z]\w*)*\z#', $potentialClassOrInterfaceName)) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I will try it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is, it replaces non existing classes as well: 9a14c3b#diff-1ca8f9425dbe99f88cb78b89459d3a6e
So I'd probably stay with *_exists()
function or do you know how to solve it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sry, the regexp should be '#^[A-Z]\w*[a-z]\w*(\\\\[A-Z]\w*[a-z]\w*)+\z#'
, ie. +
instead of *
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, it can change false-classes, but in practice it rarely happens. Regexp will not detect non-namespace classes, which are usually native PHP classes, so perhaps ideal solution is class_exists || interface_exists || regexp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixed regex, that makes sense.
Work much better now 🎂
Last thing I have to resolve is importing use statements.
…paced classes, run after NoLeadinSlashImport and after OrderedImports
9a14c3b
to
9df20a0
Compare
It looks like importing of namespaces is done by Which is run after this. Too bad |
/** | ||
* @var string | ||
*/ | ||
private const CLASS_OR_INTERFACE_PATTERN = '#^[A-Z]\w*[a-z]\w*(\\\\[A-Z]\w*[a-z]\w*)+\z#'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a lot of falsy positives here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop anything that may creates wrong changes, it's better to convert too few than too much
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example?
|
@keradus Thanks for fast reply!
|
I merged to test this in practise. Still open to feedback. |
Closes #260
Converts:
SomeClass
=>\SomeClass::class