8000 minor #34990 Use `::class` constants instead of `__NAMESPACE__` when … · linaori/symfony@cfd4194 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit cfd4194

Browse files
minor symfony#34990 Use ::class constants instead of __NAMESPACE__ when possible (fre5h)
This PR was merged into the 3.4 branch. Discussion ---------- Use `::class` constants instead of `__NAMESPACE__` when possible | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Related to symfony#34987 | License | MIT | Doc PR | no Form component has a lot of built-in form types. Some of them were implemented from the very beginning. In most of them there is a such method ```php /** * {@inheritdoc} */ public function getParent() { return __NAMESPACE__.'\TextType'; } ``` This `getParent()` method was refactored in Symfony 2.8. The upgrade instructions are given here https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md#form I think the `__NAMESPACE__.'\TextType';` expression was used because Symfony 2.8 was using `"php": ">=5.3.9"`, and the constant `::class` was added only in PHP 5.5 Now this line can be refactored into ```php /** * {@inheritdoc} */ public function getParent() { return TextType::class; } ``` For example new form types, that were added later, already using the `::class` constant. https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/ColorType.php#L23 https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/TelType.php#L23 So, in this pull request I propose to refactor all old form types to use `::class` constant. It will give a benefit during the future refactoring, because IDE or static analysers will find all usages of parent class. Unlike the `__NAMESPACE__.'\TextType';` line, which doesn't show the real link to the class for IDE or static analysers, and it could complicate finding all usages of parent class. Commits ------- 32bf50a Use `::class` constants instead of `__NAMESPACE__` when possible
2 parents a073606 + 32bf50a commit cfd4194

File tree

80 files changed

+208
-208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+208
-208
lines changed

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct($registry)
4444
public function validate($entity, Constraint $constraint)
4545
{
4646
if (!$constraint instanceof UniqueEntity) {
47-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UniqueEntity');
47+
throw new UnexpectedTypeException($constraint, UniqueEntity::class);
4848
}
4949

5050
if (!\is_array($constraint->fields) && !\is_string($constraint->fields)) {

src/Symfony/Component/Config/Definition/Builder/NodeBuilder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class NodeBuilder implements NodeParentInterface
2424
public function __construct()
2525
{
2626
$this->nodeMapping = [
27-
'variable' => __NAMESPACE__.'\\VariableNodeDefinition',
28-
'scalar' => __NAMESPACE__.'\\ScalarNodeDefinition',
29-
'boolean' => __NAMESPACE__.'\\BooleanNodeDefinition',
30-
'integer' => __NAMESPACE__.'\\IntegerNodeDefinition',
31-
'float' => __NAMESPACE__.'\\FloatNodeDefinition',
32-
'array' => __NAMESPACE__.'\\ArrayNodeDefinition',
33-
'enum' => __NAMESPACE__.'\\EnumNodeDefinition',
27+
'variable' => VariableNodeDefinition::class,
28+
'scalar' => ScalarNodeDefinition::class,
29+
'boolean' => BooleanNodeDefinition::class,
30+
'integer' => IntegerNodeDefinition::class,
31+
'float' => FloatNodeDefinition::class,
32+
'array' => ArrayNodeDefinition::class,
33+
'enum' => EnumNodeDefinition::class,
3434
];
3535
}
3636

src/Symfony/Component/Config/Tests/Definition/Builder/NodeBuilderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testThrowsAnExceptionWhenTheNodeClassIsNotFound()
3535

3636
public function testAddingANewNodeType()
3737
{
38-
$class = __NAMESPACE__.'\\SomeNodeDefinition';
38+
$class = SomeNodeDefinition::class;
3939

4040
$builder = new BaseNodeBuilder();
4141
$node = $builder
@@ -47,7 +47,7 @@ public function testAddingANewNodeType()
4747

4848
public function testOverridingAnExistingNodeType()
4949
{
50-
$class = __NAMESPACE__.'\\SomeNodeDefinition';
50+
$class = SomeNodeDefinition::class;
5151

5252
$builder = new BaseNodeBuilder();
5353
$node = $builder
@@ -66,7 +66,7 @@ public function testNodeTypesAreNotCaseSensitive()
6666

6767
$this->assertInstanceOf(\get_class($node1), $node2);
6868

69-
$builder->setNodeClass('CuStOm', __NAMESPACE__.'\\SomeNodeDefinition');
69+
$builder->setNodeClass('CuStOm', SomeNodeDefinition::class);
7070

7171
$node1 = $builder->node('', 'CUSTOM');
7272
$node2 = $builder->node('', 'custom');

src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testLoadFile()
4848
$this->assertStringContainsString('XSD file or callable', $e->getMessage());
4949
}
5050

51-
$mock = $this->getMockBuilder(__NAMESPACE__.'\Validator')->getMock();
51+
$mock = $this->getMockBuilder(Validator::class)->getMock();
5252
$mock->expects($this->exactly(2))->method('validate')->will($this->onConsecutiveCalls(false, true));
5353

5454
try {
@@ -68,7 +68,7 @@ public function testParseWithInvalidValidatorCallable()
6868
$this->expectExceptionMessage('The XML is not valid');
6969
$fixtures = __DIR__.'/../Fixtures/Util/';
7070

71-
$mock = $this->getMockBuilder(__NAMESPACE__.'\Validator')->getMock();
71+
$mock = $this->getMockBuilder(Validator::class)->getMock();
7272
$mock->expects($this->once())->method('validate')->willReturn(false);
7373

7474
XmlUtils::parse(file_get_contents($fixtures.'valid.xml'), [$mock, 'validate']);

src/Symfony/Component/Debug/Tests/DebugClassLoaderTest.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ public function testThrowingClass()
6464
$this->expectException('Exception');
6565
$this->expectExceptionMessage('boo');
6666
try {
67-
class_exists(__NAMESPACE__.'\Fixtures\Throwing');
67+
class_exists(Fixtures\Throwing::class);
6868
$this->fail('Exception expected');
6969
} catch (\Exception $e) {
7070
$this->assertSame('boo', $e->getMessage());
7171
}
7272

7373
// the second call also should throw
74-
class_exists(__NAMESPACE__.'\Fixtures\Throwing');
74+
class_exists(Fixtures\Throwing::class);
7575
}
7676

7777
public function testUnsilencing()
@@ -90,7 +90,7 @@ public function testUnsilencing()
9090

9191
// See below: this will fail with parse error
9292
// but this should not be @-silenced.
93-
@class_exists(__NAMESPACE__.'\TestingUnsilencing', true);
93+
@class_exists(TestingUnsilencing::class, true);
9494

9595
$output = ob_get_clean();
9696

@@ -141,7 +141,7 @@ public function testNameCaseMismatch()
141141
{
142142
$this->expectException('RuntimeException');
143143
$this->expectExceptionMessage('Case mismatch between loaded and declared class names');
144-
class_exists(__NAMESPACE__.'\TestingCaseMismatch', true);
144+
class_exists(TestingCaseMismatch::class, true);
145145
}
146146

147147
public function testFileCaseMismatch()
@@ -152,7 +152,7 @@ public function testFileCaseMismatch()
152152
$this->markTestSkipped('Can only be run on case insensitive filesystems');
153153
}
154154

155-
class_exists(__NAMESPACE__.'\Fixtures\CaseMismatch', true);
155+
class_exists(Fixtures\CaseMismatch::class, true);
156156
}
157157

158158
public function testPsr4CaseMismatch()
@@ -174,7 +174,7 @@ public function testNotPsr0Bis()
174174

175175
public function testClassAlias()
176176
{
177-
$this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\ClassAlias', true));
177+
$this->assertTrue(class_exists(Fixtures\ClassAlias::class, true));
178178
}
179179

180180
/**
@@ -216,7 +216,7 @@ public function testInterfaceExtendsDeprecatedInterface()
216216
$e = error_reporting(0);
217217
trigger_error('', E_USER_NOTICE);
218218

219-
class_exists('Test\\'.__NAMESPACE__.'\\NonDeprecatedInterfaceClass', true);
219+
class_exists('Test\\'.NonDeprecatedInterfaceClass::class, true);
220220

221221
error_reporting($e);
222222
restore_error_handler();
@@ -264,7 +264,7 @@ public function testReservedForPhp7()
264264
$e = error_reporting(0);
265265
trigger_error('', E_USER_NOTICE);
266266

267-
class_exists('Test\\'.__NAMESPACE__.'\\Float', true);
267+
class_exists('Test\\'.Float::class, true);
268268

269269
error_reporting($e);
270270
restore_error_handler();
@@ -289,7 +289,7 @@ public function testExtendedFinalClass()
289289
require __DIR__.'/Fixtures/FinalClasses.php';
290290

291291
$i = 1;
292-
while (class_exists($finalClass = __NAMESPACE__.'\\Fixtures\\FinalClass'.$i++, false)) {
292+
while (class_exists($finalClass = Fixtures\FinalClass::class.$i++, false)) {
293293
spl_autoload_call($finalClass);
294294
class_exists('Test\\'.__NAMESPACE__.'\\Extends'.substr($finalClass, strrpos($finalClass, '\\') + 1), true);
295295
}
@@ -315,7 +315,7 @@ public function testExtendedFinalMethod()
315315
set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
316316
$e = error_reporting(E_USER_DEPRECATED);
317317

318-
class_exists(__NAMESPACE__.'\\Fixtures\\ExtendedFinalMethod', true);
318+
class_exists(Fixtures\ExtendedFinalMethod::class, true);
319319

320320
error_reporting($e);
321321
restore_error_handler();
@@ -334,7 +334,7 @@ public function testExtendedDeprecatedMethodDoesntTriggerAnyNotice()
334334
$e = error_reporting(0);
335335
trigger_error('', E_USER_NOTICE);
336336

337-
class_exists('Test\\'.__NAMESPACE__.'\\ExtendsAnnotatedClass', true);
337+
class_exists('Test\\'.ExtendsAnnotatedClass::class, true);
338338

339339
error_reporting($e);
340340
restore_error_handler();
@@ -351,7 +351,7 @@ public function testInternalsUse()
351351
set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
352352
$e = error_reporting(E_USER_DEPRECATED);
353353

354-
class_exists('Test\\'.__NAMESPACE__.'\\ExtendsInternals', true);
354+
class_exists('Test\\'.ExtendsInternals::class, true);
355355

356356
error_reporting($e);
357357
restore_error_handler();
@@ -370,7 +370,7 @@ public function testUseTraitWithInternalMethod()
370370
set_error_handler(function ($type, $msg) use (&$deprecations) { $deprecations[] = $msg; });
371371
$e = error_reporting(E_USER_DEPRECATED);
372372

373-
class_exists('Test\\'.__NAMESPACE__.'\\UseTraitWithInternalMethod', true);
373+
class_exists('Test\\'.UseTraitWithInternalMethod::class, true);
374374

375375
error_reporting($e);
376376
restore_error_handler();
@@ -380,7 +380,7 @@ class_exists('Test\\'.__NAMESPACE__.'\\UseTraitWithInternalMethod', true);
380380

381381
public function testEvaluatedCode()
382382
{
383-
$this->assertTrue(class_exists(__NAMESPACE__.'\Fixtures\DefinitionInEvaluatedCode', true));
383+
$this->assertTrue(class_exists(Fixtures\DefinitionInEvaluatedCode::class, true));
384384
}
385385
}
386386

@@ -399,11 +399,11 @@ public function findFile($class)
399399
{
400400
$fixtureDir = __DIR__.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR;
401401

402-
if (__NAMESPACE__.'\TestingUnsilencing' === $class) {
402+
if (TestingUnsilencing::class === $class) {
403403
eval('-- parse error --');
404-
} elseif (__NAMESPACE__.'\TestingStacking' === $class) {
404+
} elseif (TestingStacking::class === $class) {
405405
eval('namespace '.__NAMESPACE__.'; class TestingStacking { function foo() {} }');
406-
} elseif (__NAMESPACE__.'\TestingCaseMismatch' === $class) {
406+
} elseif (TestingCaseMismatch::class === $class) {
407407
eval('namespace '.__NAMESPACE__.'; class TestingCaseMisMatch {}');
408408
} elseif (__NAMESPACE__.'\Fixtures\Psr4CaseMismatch' === $class) {
409409
return $fixtureDir.'psr4'.\DIRECTORY_SEPARATOR.'Psr4CaseMismatch.php';
@@ -413,30 +413,30 @@ public function findFile($class)
413413
return $fixtureDir.'notPsr0Bis.php';
414414
} elseif ('Symfony\Bridge\Debug\Tests\Fixtures\ExtendsDeprecatedParent' === $class) {
415415
eval('namespace Symfony\Bridge\Debug\Tests\Fixtures; class ExtendsDeprecatedParent extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
416-
} elseif ('Test\\'.__NAMESPACE__.'\DeprecatedParentClass' === $class) {
416+
} elseif ('Test\\'.DeprecatedParentClass::class === $class) {
417417
eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedParentClass extends \\'.__NAMESPACE__.'\Fixtures\DeprecatedClass {}');
418-
} elseif ('Test\\'.__NAMESPACE__.'\DeprecatedInterfaceClass' === $class) {
418+
} elseif ('Test\\'.DeprecatedInterfaceClass::class === $class) {
419419
eval('namespace Test\\'.__NAMESPACE__.'; class DeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\DeprecatedInterface {}');
420-
} elseif ('Test\\'.__NAMESPACE__.'\NonDeprecatedInterfaceClass' === $class) {
420+
} elseif ('Test\\'.NonDeprecatedInterfaceClass::class === $class) {
421421
eval('namespace Test\\'.__NAMESPACE__.'; class NonDeprecatedInterfaceClass implements \\'.__NAMESPACE__.'\Fixtures\NonDeprecatedInterface {}');
422-
} elseif ('Test\\'.__NAMESPACE__.'\Float' === $class) {
422+
} elseif ('Test\\'.Float::class === $class) {
423423
eval('namespace Test\\'.__NAMESPACE__.'; class Float {}');
424-
} elseif (0 === strpos($class, 'Test\\'.__NAMESPACE__.'\ExtendsFinalClass')) {
424+
} elseif (0 === strpos($class, 'Test\\'.ExtendsFinalClass::class)) {
425425
$classShortName = substr($class, strrpos($class, '\\') + 1);
426426
eval('namespace Test\\'.__NAMESPACE__.'; class '.$classShortName.' extends \\'.__NAMESPACE__.'\Fixtures\\'.substr($classShortName, 7).' {}');
427-
} elseif ('Test\\'.__NAMESPACE__.'\ExtendsAnnotatedClass' === $class) {
427+
} elseif ('Test\\'.ExtendsAnnotatedClass::class === $class) {
428428
eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsAnnotatedClass extends \\'.__NAMESPACE__.'\Fixtures\AnnotatedClass {
429429
public function deprecatedMethod() { }
430430
}');
431-
} elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternals' === $class) {
431+
} elseif ('Test\\'.ExtendsInternals::class === $class) {
432432
eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternals extends ExtendsInternalsParent {
433433
use \\'.__NAMESPACE__.'\Fixtures\InternalTrait;
434434
435435
public function internalMethod() { }
436436
}');
437-
} elseif ('Test\\'.__NAMESPACE__.'\ExtendsInternalsParent' === $class) {
437+
} elseif ('Test\\'.ExtendsInternalsParent::class === $class) {
438438
eval('namespace Test\\'.__NAMESPACE__.'; class ExtendsInternalsParent extends \\'.__NAMESPACE__.'\Fixtures\InternalClass implements \\'.__NAMESPACE__.'\Fixtures\InternalInterface { }');
439-
} elseif ('Test\\'.__NAMESPACE__.'\UseTraitWithInternalMethod' === $class) {
439+
} elseif ('Test\\'.UseTraitWithInternalMethod::class === $class) {
440440
eval('namespace Test\\'.__NAMESPACE__.'; class UseTraitWithInternalMethod { use \\'.__NAMESPACE__.'\Fixtures\TraitWithInternalMethod; }');
441441
}
442442

0 commit comments

Comments
 (0)
0