|
| 1 | +--TEST-- |
| 2 | +ReflectionTypes of relative class types (self, parent) from traits in 2 different unrelated classes, parameter types |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +trait TraitExample { |
| 7 | + public function bar(parent $o) { return $o; } |
| 8 | + public function ping(self $o) { return $o; } |
| 9 | + //public function barNull(?parent $o) { return $o; } |
| 10 | + //public function pingNull(?self $o) { return $o; } |
| 11 | +} |
| 12 | + |
| 13 | +class A { |
| 14 | + |
| 15 | +} |
| 16 | + |
| 17 | +class B extends A { |
| 18 | +} |
| 19 | + |
| 20 | +class C extends B { |
| 21 | + use TraitExample; |
| 22 | +} |
| 23 | + |
| 24 | +class D extends C {} |
| 25 | + |
| 26 | +class A2 { |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +class B2 extends A2 { |
| 31 | +} |
| 32 | + |
| 33 | +class C2 extends B2 { |
| 34 | + use TraitExample; |
| 35 | +} |
| 36 | + |
| 37 | +class D2 extends C2 {} |
| 38 | + |
| 39 | +$instances = [ |
| 40 | + new A, |
| 41 | + new B, |
| 42 | + new C, |
| 43 | + new D, |
| 44 | + new A2, |
| 45 | + new B2, |
| 46 | + new C2, |
| 47 | + new D2, |
| 48 | +]; |
| 49 | + |
| 50 | +foreach ($instances as $instance) { |
| 51 | + echo 'Class: ', $instance::class, PHP_EOL; |
| 52 | + $rc = new ReflectionClass($instance); |
| 53 | + $methods = $rc->getMethods(); |
| 54 | + foreach ($methods as $method) { |
| 55 | + echo "\tMethod: ", $method->name, PHP_EOL; |
| 56 | + $parameters = $method->getParameters(); |
| 57 | + foreach ($parameters as $param) { |
| 58 | + $type = $param->getType(); |
| 59 | + echo "\t\tType: ", $type, PHP_EOL; |
| 60 | + echo "\t\tInstance of: ", $type::class, PHP_EOL; |
| 61 | + $resolvedType = $type->resolveToNamedType(); |
| 62 | + echo "\t\t\tResolved Type: ", $resolvedType, PHP_EOL; |
| 63 | + echo "\t\t\tInstance of: ", $resolvedType::class, PHP_EOL; |
| 64 | + |
| 65 | + foreach ($instances as $arg) { |
| 66 | + try { |
| 67 | + $instance->{$method->name}($arg); |
| 68 | + } catch (\TypeError $e) { |
| 69 | + echo "\t\t\t\t", $e->getMessage(), PHP_EOL; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +?> |
| 77 | +--EXPECTF-- |
| 78 | +Class: A |
| 79 | +Class: B |
| 80 | +Class: C |
| 81 | + Method: bar |
| 82 | + Type: parent |
| 83 | + Instance of: ReflectionRelativeClassType |
| 84 | + Resolved Type: B |
| 85 | + Instance of: ReflectionNamedType |
| 86 | + C::bar(): Argument #1 ($o) must be of type B, A given, called in %s on line %d |
| 87 | + C::bar(): Argument #1 ($o) must be of type B, A2 given, called in %s on line %d |
| 88 | + C::bar(): Argument #1 ($o) must be of type B, B2 given, called in %s on line %d |
| 89 | + C::bar(): Argument #1 ($o) must be of type B, C2 given, called in %s on line %d |
| 90 | + C::bar(): Argument #1 ($o) must be of type B, D2 given, called in %s on line %d |
| 91 | + Method: ping |
| 92 | + Type: self |
| 93 | + Instance of: ReflectionRelativeClassType |
| 94 | + Resolved Type: C |
| 95 | + Instance of: ReflectionNamedType |
| 96 | + C::ping(): Argument #1 ($o) must be of type C, A given, called in %s on line %d |
| 97 | + C::ping(): Argument #1 ($o) must be of type C, B given, called in %s on line %d |
| 98 | + C::ping(): Argument #1 ($o) must be of type C, A2 given, called in %s on line %d |
| 99 | + C::ping(): Argument #1 ($o) must be of type C, B2 given, called in %s on line %d |
| 100 | + C::ping(): Argument #1 ($o) must be of type C, C2 given, called in %s on line %d |
| 101 | + C::ping(): Argument #1 ($o) must be of type C, D2 given, called in %s on line %d |
| 102 | +Class: D |
| 103 | + Method: bar |
| 104 | + Type: parent |
| 105 | + Instance of: ReflectionRelativeClassType |
| 106 | + Resolved Type: B |
| 107 | + Instance of: ReflectionNamedType |
| 108 | + C::bar(): Argument #1 ($o) must be of type B, A given, called in %s on line %d |
| 109 | + C::bar(): Argument #1 ($o) must be of type B, A2 given, called in %s on line %d |
| 110 | + C::bar(): Argument #1 ($o) must be of type B, B2 given, called in %s on line %d |
| 111 | + C::bar(): Argument #1 ($o) must be of type B, C2 given, called in %s on line %d |
| 112 | + C::bar(): Argument #1 ($o) must be of type B, D2 given, called in %s on line %d |
| 113 | + Method: ping |
| 114 | + Type: self |
| 115 | + Instance of: ReflectionRelativeClassType |
| 116 | + Resolved Type: C |
| 117 | + Instance of: ReflectionNamedType |
| 118 | + C::ping(): Argument #1 ($o) must be of type C, A given, called in %s on line %d |
| 119 | + C::ping(): Argument #1 ($o) must be of type C, B given, called in %s on line %d |
| 120 | + C::ping(): Argument #1 ($o) must be of type C, A2 given, called in %s on line %d |
| 121 | + C::ping(): Argument #1 ($o) must be of type C, B2 given, called in %s on line %d |
| 122 | + C::ping(): Argument #1 ($o) must be of type C, C2 given, called in %s on line %d |
| 123 | + C::ping(): Argument #1 ($o) must be of type C, D2 given, called in %s on line %d |
| 124 | +Class: A2 |
| 125 | +Class: B2 |
| 126 | +Class: C2 |
| 127 | + Method: bar |
| 128 | + Type: parent |
| 129 | + Instance of: ReflectionRelativeClassType |
| 130 | + Resolved Type: B2 |
| 131 | + Instance of: ReflectionNamedType |
| 132 | + C2::bar(): Argument #1 ($o) must be of type B2, A given, called in %s on line %d |
| 133 | + C2::bar(): Argument #1 ($o) must be of type B2, B given, called in %s on line %d |
| 134 | + C2::bar(): Argument #1 ($o) must be of type B2, C given, called in %s on line %d |
| 135 | + C2::bar(): Argument #1 ($o) must be of type B2, D given, called in %s on line %d |
| 136 | + C2::bar(): Argument #1 ($o) must be of type B2, A2 given, called in %s on line %d |
| 137 | + Method: ping |
| 138 | + Type: self |
| 139 | + Instance of: ReflectionRelativeClassType |
| 140 | + Resolved Type: C2 |
| 141 | + Instance of: ReflectionNamedType |
| 142 | + C2::ping(): Argument #1 ($o) must be of type C2, A given, called in %s on line %d |
| 143 | + C2::ping(): Argument #1 ($o) must be of type C2, B given, called in %s on line %d |
| 144 | + C2::ping(): Argument #1 ($o) must be of type C2, C given, called in %s on line %d |
| 145 | + C2::ping(): Argument #1 ($o) must be of type C2, D given, called in %s on line %d |
| 146 | + C2::ping(): Argument #1 ($o) must be of type C2, A2 given, called in %s on line %d |
| 147 | + C2::ping(): Argument #1 ($o) must be of type C2, B2 given, called in %s on line %d |
| 148 | +Class: D2 |
| 149 | + Method: bar |
| 150 | + Type: parent |
| 151 | + Instance of: ReflectionRelativeClassType |
| 152 | + Resolved Type: B2 |
| 153 | + Instance of: ReflectionNamedType |
| 154 | + C2::bar(): Argument #1 ($o) must be of type B2, A given, called in %s on line %d |
| 155 | + C2::bar(): Argument #1 ($o) must be of type B2, B given, called in %s on line %d |
| 156 | + C2::bar(): Argument #1 ($o) must be of type B2, C given, called in %s on line %d |
| 157 | + C2::bar(): Argument #1 ($o) must be of type B2, D given, called in %s on line %d |
| 158 | + C2::bar(): Argument #1 ($o) must be of type B2, A2 given, called in %s on line %d |
| 159 | + Method: ping |
| 160 | + Type: self |
| 161 | + Instance of: ReflectionRelativeClassType |
| 162 | + Resolved Type: C2 |
| 163 | + Instance of: ReflectionNamedType |
| 164 | + C2::ping(): Argument #1 ($o) must be of type C2, A given, called in %s on line %d |
| 165 | + C2::ping(): Argument #1 ($o) must be of type C2, B given, called in %s on line %d |
| 166 | + C2::ping(): Argument #1 ($o) must be of type C2, C given, called in %s on line %d |
| 167 | + C2::ping(): Argument #1 ($o) must be of type C2, D given, called in %s on line %d |
| 168 | + C2::ping(): Argument #1 ($o) must be of type C2, A2 given, called in %s on line %d |
| 169 | + C2::ping(): Argument #1 ($o) must be of type C2, B2 given, called in %s on line %d |
0 commit comments