Skip to content

Commit 12288a1

Browse files
authored
Merge pull request #907 from kukulich/old
Revert "Removed support for PHP4 style constructors"
2 parents 2b64de3 + 5914a9b commit 12288a1

File tree

7 files changed

+107
-2
lines changed

7 files changed

+107
-2
lines changed

UPGRADE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ or backwards compatibility (BC) breakages occur.
1616
* Method `\Roave\BetterReflection\Identifier\IdentifierType::isMatchingReflector()` has been removed.
1717
* All adapters are `final`
1818
* `ClassReflector`, `FunctionReflector` and `ConstantReflector` have been removed. Use `DefaultReflector` to reflect all types.
19-
* Removed support for PHP4 style constructors
2019
* Adapters don't have `::export()` method anymore because these methods were removed from PHP. These methods have been removed:
2120
* `\Roave\BetterReflection\Reflection\Adapter\ReflectionClass::export()`
2221
* `\Roave\BetterReflection\Reflection\Adapter\ReflectionFunction::export()`

src/Reflection/ReflectionMethod.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,16 @@ public function isStatic(): bool
260260
*/
261261
public function isConstructor(): bool
262262
{
263-
return strtolower($this->getName()) === '__construct';
263+
if (strtolower($this->getName()) === '__construct') {
264+
return true;
265+
}
266+
267+
$declaringClass = $this->getDeclaringClass();
268+
if ($declaringClass->inNamespace()) {
269+
return false;
270+
}
271+
272+
return strtolower($this->getName()) === strtolower($declaringClass->getShortName());
264273
}
265274

266275
/**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
class Php4StyleCaseInsensitiveConstruct
4+
{
5+
public function PHP4STYLECASEINSENSITIVECONSTRUCT()
6+
{
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
class Php4StyleConstruct
4+
{
5+
public function Php4StyleConstruct()
6+
{
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Roave\BetterReflectionTest\Fixture;
4+
5+
class Php4StyleConstructInNamespace
6+
{
7+
public function Php4StyleConstructInNamespace()
8+
{
9+
}
10+
}

test/unit/Reflection/ReflectionClassTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Baz;
1010
use E;
1111
use Iterator;
12+
use OutOfBoundsException;
13+
use Php4StyleCaseInsensitiveConstruct;
14+
use Php4StyleConstruct;
1215
use PhpParser\Node\Stmt\Class_;
1316
use PHPUnit\Framework\TestCase;
1417
use Qux;
@@ -494,6 +497,44 @@ public function testGetCaseInsensitiveConstructor(): void
494497
self::assertTrue($constructor->isConstructor());
495498
}
496499

500+
public function testGetConstructorWhenPhp4Style(): void
501+
{
502+
$classInfo = (new DefaultReflector(new SingleFileSourceLocator(
503+
__DIR__ . '/../Fixture/Php4StyleConstruct.php',
504+
$this->astLocator,
505+
)))->reflectClass(Php4StyleConstruct::class);
506+
507+
$constructor = $classInfo->getConstructor();
508+
509+
self::assertInstanceOf(ReflectionMethod::class, $constructor);
510+
self::assertTrue($constructor->isConstructor());
511+
}
512+
513+
public function testGetConstructorWhenPhp4StyleInNamespace(): void
514+
{
515+
$this->expectException(OutOfBoundsException::class);
516+
517+
$classInfo = (new DefaultReflector(new SingleFileSourceLocator(
518+
__DIR__ . '/../Fixture/Php4StyleConstructInNamespace.php',
519+
$this->astLocator,
520+
)))->reflectClass(Fixture\Php4StyleConstructInNamespace::class);
521+
522+
$classInfo->getConstructor();
523+
}
524+
525+
public function testGetConstructorWhenPhp4StyleCaseInsensitive(): void
526+
{
527+
$classInfo = (new DefaultReflector(new SingleFileSourceLocator(
528+
__DIR__ . '/../Fixture/Php4StyleCaseInsensitiveConstruct.php',
529+
$this->astLocator,
530+
)))->reflectClass(Php4StyleCaseInsensitiveConstruct::class);
531+
532+
$constructor = $classInfo->getConstructor();
533+
534+
self::assertInstanceOf(ReflectionMethod::class, $constructor);
535+
self::assertTrue($constructor->isConstructor());
536+
}
537+
497538
public function testGetProperties(): void
498539
{
499540
$reflector = new DefaultReflector($this->getComposerLocator());

test/unit/Reflection/ReflectionMethodTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use ClassWithMethodsAndTraitMethods;
99
use Closure;
1010
use ExtendedClassWithMethodsAndTraitMethods;
11+
use Php4StyleCaseInsensitiveConstruct;
12+
use Php4StyleConstruct;
1113
use PHPUnit\Framework\TestCase;
1214
use Reflection;
1315
use ReflectionClass;
@@ -36,6 +38,7 @@
3638
use Roave\BetterReflectionTest\Fixture\ExampleClass;
3739
use Roave\BetterReflectionTest\Fixture\InterfaceWithMethod;
3840
use Roave\BetterReflectionTest\Fixture\Methods;
41+
use Roave\BetterReflectionTest\Fixture\Php4StyleConstructInNamespace;
3942
use Roave\BetterReflectionTest\Fixture\TraitWithStaticMethod;
4043
use Roave\BetterReflectionTest\Fixture\TraitWithStaticMethodToUse;
4144
use Roave\BetterReflectionTest\Fixture\UpperCaseConstructDestruct;
@@ -161,6 +164,33 @@ public function testIsConstructorDestructorIsCaseInsensitive(): void
161164
self::assertTrue($method->isDestructor());
162165
}
163166

167+
public function testIsConstructorWhenPhp4Style(): void
168+
{
169+
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php4StyleConstruct.php', $this->astLocator));
170+
$classInfo = $reflector->reflectClass(Php4StyleConstruct::class);
171+
172+
$method = $classInfo->getMethod('Php4StyleConstruct');
173+
self::assertTrue($method->isConstructor());
174+
}
175+
176+
public function testsIsConstructorWhenPhp4StyleInNamespace(): void
177+
{
178+
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php4StyleConstructInNamespace.php', $this->astLocator));
179+
$classInfo = $reflector->reflectClass(Php4StyleConstructInNamespace::class);
180+
181+
$method = $classInfo->getMethod('Php4StyleConstructInNamespace');
182+
self::assertFalse($method->isConstructor());
183+
}
184+
185+
public function testIsConstructorWhenPhp4StyleCaseInsensitive(): void
186+
{
187+
$reflector = new DefaultReflector(new SingleFileSourceLocator(__DIR__ . '/../Fixture/Php4StyleCaseInsensitiveConstruct.php', $this->astLocator));
188+
$classInfo = $reflector->reflectClass(Php4StyleCaseInsensitiveConstruct::class);
189+
190+
$method = $classInfo->getMethod('PHP4STYLECASEINSENSITIVECONSTRUCT');
191+
self::assertTrue($method->isConstructor());
192+
}
193+
164194
public function testGetParameters(): void
165195
{
166196
$classInfo = $this->reflector->reflectClass(Methods::class);

0 commit comments

Comments
 (0)