 Ocramius    /
                    ProxyManager
                      Ocramius    /
                    ProxyManager
                
                            This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
                                via PHP's auto-loading mechanism.
                                                    These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace ProxyManagerTest\ProxyGenerator\LazyLoadingGhost\MethodGenerator; | ||
| 6 | |||
| 7 | use Laminas\Code\Generator\MethodGenerator; | ||
| 8 | use Laminas\Code\Generator\PropertyGenerator; | ||
| 9 | use PHPUnit\Framework\MockObject\MockObject; | ||
| 10 | use PHPUnit\Framework\TestCase; | ||
| 11 | use ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicUnset; | ||
| 12 | use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\PrivatePropertiesMap; | ||
| 13 | use ProxyManager\ProxyGenerator\LazyLoadingGhost\PropertyGenerator\ProtectedPropertiesMap; | ||
| 14 | use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap; | ||
| 15 | use ProxyManagerTestAsset\ClassWithMagicMethods; | ||
| 16 | use ProxyManagerTestAsset\ProxyGenerator\LazyLoading\MethodGenerator\ClassWithTwoPublicProperties; | ||
| 17 | use ReflectionClass; | ||
| 18 | |||
| 19 | /** | ||
| 20 |  * Tests for {@see \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicUnset} | ||
| 21 | * | ||
| 22 | * @group Coverage | ||
| 23 | */ | ||
| 24 | final class MagicUnsetTest extends TestCase | ||
| 25 | { | ||
| 26 | /** @var PropertyGenerator&MockObject */ | ||
| 27 | private PropertyGenerator $initializer; | ||
| 0 ignored issues–
                            show             Bug
    
    
    
        introduced 
                            by  
  Loading history... | |||
| 28 | |||
| 29 | /** @var MethodGenerator&MockObject */ | ||
| 30 | private MethodGenerator $initMethod; | ||
| 31 | |||
| 32 | /** @var PublicPropertiesMap&MockObject */ | ||
| 33 | private PublicPropertiesMap $publicProperties; | ||
| 34 | |||
| 35 | /** @var ProtectedPropertiesMap&MockObject */ | ||
| 36 | private ProtectedPropertiesMap $protectedProperties; | ||
| 37 | |||
| 38 | /** @var PrivatePropertiesMap&MockObject */ | ||
| 39 | private PrivatePropertiesMap $privateProperties; | ||
| 40 | |||
| 41 | private string $expectedCode = <<<'PHP' | ||
| 42 | $this->foo && $this->baz('__unset', array('name' => $name)); | ||
| 43 | |||
| 44 | if (isset(self::$bar[$name])) { | ||
| 45 | unset($this->$name); | ||
| 46 | |||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (isset(self::$baz[$name])) { | ||
| 51 | // check protected property access via compatible class | ||
| 52 | $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); | ||
| 53 | $caller = isset($callers[1]) ? $callers[1] : []; | ||
| 54 | $object = isset($caller['object']) ? $caller['object'] : ''; | ||
| 55 | $expectedType = self::$baz[$name]; | ||
| 56 | |||
| 57 |     if ($object instanceof $expectedType) { | ||
| 58 | unset($this->$name); | ||
| 59 | |||
| 60 | return; | ||
| 61 | } | ||
| 62 | |||
| 63 | $class = isset($caller['class']) ? $caller['class'] : ''; | ||
| 64 | |||
| 65 |     if ($class === $expectedType || is_subclass_of($class, $expectedType) || $class === 'ReflectionProperty') { | ||
| 66 | unset($this->$name); | ||
| 67 | |||
| 68 | return; | ||
| 69 | } | ||
| 70 | } elseif (isset(self::$tab[$name])) { | ||
| 71 | // check private property access via same class | ||
| 72 | $callers = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT, 2); | ||
| 73 | $caller = isset($callers[1]) ? $callers[1] : []; | ||
| 74 | $class = isset($caller['class']) ? $caller['class'] : ''; | ||
| 75 | |||
| 76 | static $accessorCache = []; | ||
| 77 | |||
| 78 |     if (isset(self::$tab[$name][$class])) { | ||
| 79 | $cacheKey = $class . '#' . $name; | ||
| 80 | $accessor = isset($accessorCache[$cacheKey]) | ||
| 81 | ? $accessorCache[$cacheKey] | ||
| 82 |             : $accessorCache[$cacheKey] = \Closure::bind(static function ($instance) use ($name) { | ||
| 83 | unset($instance->$name); | ||
| 84 | }, null, $class); | ||
| 85 | |||
| 86 | return $accessor($this); | ||
| 87 | } | ||
| 88 | |||
| 89 |     if ('ReflectionProperty' === $class) { | ||
| 90 | $tmpClass = key(self::$tab[$name]); | ||
| 91 | $cacheKey = $tmpClass . '#' . $name; | ||
| 92 | $accessor = isset($accessorCache[$cacheKey]) | ||
| 93 | ? $accessorCache[$cacheKey] | ||
| 94 |             : $accessorCache[$cacheKey] = \Closure::bind(static function ($instance) use ($name) { | ||
| 95 | unset($instance->$name); | ||
| 96 | }, null, $tmpClass); | ||
| 97 | |||
| 98 | return $accessor($this); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | %A | ||
| 102 | PHP; | ||
| 103 | |||
| 104 | /** | ||
| 105 |      * {@inheritDoc} | ||
| 106 | */ | ||
| 107 | protected function setUp() : void | ||
| 108 |     { | ||
| 109 | $this->initializer = $this->createMock(PropertyGenerator::class); | ||
| 110 | $this->initMethod = $this->createMock(MethodGenerator::class); | ||
| 111 | $this->publicProperties = $this->createMock(PublicPropertiesMap::class); | ||
| 112 | $this->protectedProperties = $this->createMock(ProtectedPropertiesMap::class); | ||
| 113 | $this->privateProperties = $this->createMock(PrivatePropertiesMap::class); | ||
| 114 | |||
| 115 |         $this->initializer->method('getName')->willReturn('foo'); | ||
| 116 |         $this->initMethod->method('getName')->willReturn('baz'); | ||
| 117 |         $this->publicProperties->method('isEmpty')->willReturn(false); | ||
| 118 |         $this->publicProperties->method('getName')->willReturn('bar'); | ||
| 119 |         $this->protectedProperties->method('getName')->willReturn('baz'); | ||
| 120 |         $this->privateProperties->method('getName')->willReturn('tab'); | ||
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicUnset::__construct | ||
| 125 | */ | ||
| 126 | public function testBodyStructure() : void | ||
| 127 |     { | ||
| 128 | $magicIsset = new MagicUnset( | ||
| 129 | new ReflectionClass(ClassWithTwoPublicProperties::class), | ||
| 130 | $this->initializer, | ||
| 131 | $this->initMethod, | ||
| 132 | $this->publicProperties, | ||
| 133 | $this->protectedProperties, | ||
| 134 | $this->privateProperties | ||
| 135 | ); | ||
| 136 | |||
| 137 |         self::assertSame('__unset', $magicIsset->getName()); | ||
| 138 | self::assertCount(1, $magicIsset->getParameters()); | ||
| 139 | self::assertStringMatchesFormat($this->expectedCode, $magicIsset->getBody()); | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * @covers \ProxyManager\ProxyGenerator\LazyLoadingGhost\MethodGenerator\MagicUnset::__construct | ||
| 144 | */ | ||
| 145 | public function testBodyStructureWithOverriddenMagicGet() : void | ||
| 146 |     { | ||
| 147 | $magicIsset = new MagicUnset( | ||
| 148 | new ReflectionClass(ClassWithMagicMethods::class), | ||
| 149 | $this->initializer, | ||
| 150 | $this->initMethod, | ||
| 151 | $this->publicProperties, | ||
| 152 | $this->protectedProperties, | ||
| 153 | $this->privateProperties | ||
| 154 | ); | ||
| 155 | |||
| 156 |         self::assertSame('__unset', $magicIsset->getName()); | ||
| 157 | self::assertCount(1, $magicIsset->getParameters()); | ||
| 158 | |||
| 159 | $body = $magicIsset->getBody(); | ||
| 160 | |||
| 161 | self::assertStringMatchesFormat($this->expectedCode, $body); | ||
| 162 |         self::assertStringMatchesFormat('%Areturn parent::__unset($name);', $body); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | 
