@@ -105,11 +105,14 @@ |
||
| 105 | 105 | $definition = \iterator_to_array((new AttributesParser(new AttributeReader())) |
| 106 | 106 | ->parse(new $listener))[0]; |
| 107 | 107 | |
| 108 | - if ($definition->scope === null) { |
|
| 108 | + if ($definition->scope === null) |
|
| 109 | + { |
|
| 109 | 110 | $this->classes |
| 110 | 111 | ->shouldReceive('getClasses') |
| 111 | 112 | ->andReturn($classes); |
| 112 | - } else { |
|
| 113 | + } |
|
| 114 | + else |
|
| 115 | + { |
|
| 113 | 116 | $this->scopedClasses |
| 114 | 117 | ->shouldReceive('getScopedClasses') |
| 115 | 118 | ->with($definition->scope) |
@@ -30,7 +30,8 @@ |
||
| 30 | 30 | |
| 31 | 31 | $listener = \Mockery::mock(TokenizationListenerInterface::class); |
| 32 | 32 | |
| 33 | - foreach ($classes as $class) { |
|
| 33 | + foreach ($classes as $class) |
|
| 34 | + { |
|
| 34 | 35 | $listener->shouldReceive('listen') |
| 35 | 36 | ->once() |
| 36 | 37 | ->with($class); |
@@ -57,15 +57,18 @@ discard block |
||
| 57 | 57 | ): void { |
| 58 | 58 | // First, we check if the listener has been cached. If it has, we will load the classes |
| 59 | 59 | // from the cache. |
| 60 | - foreach ($this->listeners as $i => $listener) { |
|
| 61 | - if ($cachedClassesLoader->loadClasses($listener)) { |
|
| 60 | + foreach ($this->listeners as $i => $listener) |
|
| 61 | + { |
|
| 62 | + if ($cachedClassesLoader->loadClasses($listener)) |
|
| 63 | + { |
|
| 62 | 64 | unset($this->listeners[$i]); |
| 63 | 65 | } |
| 64 | 66 | } |
| 65 | 67 | |
| 66 | 68 | // If there are no listeners left, we don't need to use static analysis at all and save |
| 67 | 69 | // valuable time. |
| 68 | - if ($this->listeners === []) { |
|
| 70 | + if ($this->listeners === []) |
|
| 71 | + { |
|
| 69 | 72 | return; |
| 70 | 73 | } |
| 71 | 74 | |
@@ -73,7 +76,8 @@ discard block |
||
| 73 | 76 | // Please note that this is a very expensive operation and should be avoided if possible. |
| 74 | 77 | // Use #[ListenForClasses] attribute in your listeners to cache the classes. |
| 75 | 78 | $classes = $classes->getClasses(); |
| 76 | - foreach ($this->listeners as $listener) { |
|
| 79 | + foreach ($this->listeners as $listener) |
|
| 80 | + { |
|
| 77 | 81 | $invoker->invoke($listener, $classes); |
| 78 | 82 | } |
| 79 | 83 | |
@@ -45,16 +45,20 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | private function getClassesForClass(array $classes, \ReflectionClass $target): \Generator |
| 47 | 47 | { |
| 48 | - foreach ($classes as $class) { |
|
| 49 | - if (!$target->isTrait()) { |
|
| 50 | - if ($class->isSubclassOf($target) || $class->getName() === $target->getName()) { |
|
| 48 | + foreach ($classes as $class) |
|
| 49 | + { |
|
| 50 | + if (!$target->isTrait()) |
|
| 51 | + { |
|
| 52 | + if ($class->isSubclassOf($target) || $class->getName() === $target->getName()) |
|
| 53 | + { |
|
| 51 | 54 | yield $class->getName(); |
| 52 | 55 | continue; |
| 53 | 56 | } |
| 54 | 57 | } |
| 55 | 58 | |
| 56 | 59 | // Checking using traits |
| 57 | - if (\in_array($target->getName(), $this->fetchTraits($class->getName()))) { |
|
| 60 | + if (\in_array($target->getName(), $this->fetchTraits($class->getName()))) |
|
| 61 | + { |
|
| 58 | 62 | yield $class->getName(); |
| 59 | 63 | } |
| 60 | 64 | } |
@@ -68,7 +72,8 @@ discard block |
||
| 68 | 72 | */ |
| 69 | 73 | private function getClassesForAttribute(array $classes, \ReflectionClass $target, \Attribute $attribute): \Generator |
| 70 | 74 | { |
| 71 | - foreach ($classes as $class) { |
|
| 75 | + foreach ($classes as $class) |
|
| 76 | + { |
|
| 72 | 77 | // If attribute is defined on class level and class has target attribute |
| 73 | 78 | // then we can add it to the list of classes |
| 74 | 79 | if (($attribute->flags & \Attribute::TARGET_CLASS) |
@@ -80,9 +85,12 @@ discard block |
||
| 80 | 85 | |
| 81 | 86 | // If attribute is defined on method level and class methods has target attribute |
| 82 | 87 | // then we can add it to the list of classes |
| 83 | - if ($attribute->flags & \Attribute::TARGET_METHOD) { |
|
| 84 | - foreach ($class->getMethods() as $method) { |
|
| 85 | - if ($this->reader->firstFunctionMetadata($method, $target->getName())) { |
|
| 88 | + if ($attribute->flags & \Attribute::TARGET_METHOD) |
|
| 89 | + { |
|
| 90 | + foreach ($class->getMethods() as $method) |
|
| 91 | + { |
|
| 92 | + if ($this->reader->firstFunctionMetadata($method, $target->getName())) |
|
| 93 | + { |
|
| 86 | 94 | yield $class->getName(); |
| 87 | 95 | continue 2; |
| 88 | 96 | } |
@@ -91,9 +99,12 @@ discard block |
||
| 91 | 99 | |
| 92 | 100 | // If attribute is defined on property level and class properties has target attribute |
| 93 | 101 | // then we can add it to the list of classes |
| 94 | - if ($attribute->flags & \Attribute::TARGET_PROPERTY) { |
|
| 95 | - foreach ($class->getProperties() as $property) { |
|
| 96 | - if ($this->reader->firstPropertyMetadata($property, $target->getName())) { |
|
| 102 | + if ($attribute->flags & \Attribute::TARGET_PROPERTY) |
|
| 103 | + { |
|
| 104 | + foreach ($class->getProperties() as $property) |
|
| 105 | + { |
|
| 106 | + if ($this->reader->firstPropertyMetadata($property, $target->getName())) |
|
| 107 | + { |
|
| 97 | 108 | yield $class->getName(); |
| 98 | 109 | continue 2; |
| 99 | 110 | } |
@@ -103,9 +114,12 @@ discard block |
||
| 103 | 114 | |
| 104 | 115 | // If attribute is defined on constant level and class constants has target attribute |
| 105 | 116 | // then we can add it to the list of classes |
| 106 | - if ($attribute->flags & \Attribute::TARGET_CLASS_CONSTANT) { |
|
| 107 | - foreach ($class->getReflectionConstants() as $constant) { |
|
| 108 | - if ($this->reader->firstConstantMetadata($constant, $target->getName())) { |
|
| 117 | + if ($attribute->flags & \Attribute::TARGET_CLASS_CONSTANT) |
|
| 118 | + { |
|
| 119 | + foreach ($class->getReflectionConstants() as $constant) |
|
| 120 | + { |
|
| 121 | + if ($this->reader->firstConstantMetadata($constant, $target->getName())) |
|
| 122 | + { |
|
| 109 | 123 | yield $class->getName(); |
| 110 | 124 | continue 2; |
| 111 | 125 | } |
@@ -115,10 +129,14 @@ discard block |
||
| 115 | 129 | |
| 116 | 130 | // If attribute is defined on method parameters level and class method parameter has target attribute |
| 117 | 131 | // then we can add it to the list of classes |
| 118 | - if ($attribute->flags & \Attribute::TARGET_PARAMETER) { |
|
| 119 | - foreach ($class->getMethods() as $method) { |
|
| 120 | - foreach ($method->getParameters() as $parameter) { |
|
| 121 | - if ($this->reader->firstParameterMetadata($parameter, $target->getName())) { |
|
| 132 | + if ($attribute->flags & \Attribute::TARGET_PARAMETER) |
|
| 133 | + { |
|
| 134 | + foreach ($class->getMethods() as $method) |
|
| 135 | + { |
|
| 136 | + foreach ($method->getParameters() as $parameter) |
|
| 137 | + { |
|
| 138 | + if ($this->reader->firstParameterMetadata($parameter, $target->getName())) |
|
| 139 | + { |
|
| 122 | 140 | yield $class->getName(); |
| 123 | 141 | continue 3; |
| 124 | 142 | } |
@@ -13,7 +13,8 @@ |
||
| 13 | 13 | { |
| 14 | 14 | public function invoke(TokenizationListenerInterface $listener, iterable $classes): void |
| 15 | 15 | { |
| 16 | - foreach ($classes as $class) { |
|
| 16 | + foreach ($classes as $class) |
|
| 17 | + { |
|
| 17 | 18 | $listener->listen($class); |
| 18 | 19 | } |
| 19 | 20 | |
@@ -22,7 +22,8 @@ discard block |
||
| 22 | 22 | $definitions = \iterator_to_array($this->parser->parse($listener)); |
| 23 | 23 | |
| 24 | 24 | // If there are no definitions, then listener can't be cached. |
| 25 | - if ($definitions === []) { |
|
| 25 | + if ($definitions === []) |
|
| 26 | + { |
|
| 26 | 27 | return false; |
| 27 | 28 | } |
| 28 | 29 | |
@@ -31,9 +32,11 @@ discard block |
||
| 31 | 32 | // We decided to load classes for each definition separately. |
| 32 | 33 | // It allows us to cache classes for each definition separately and if we reuse the |
| 33 | 34 | // same target in multiple listeners, we will not have to load classes for the same target. |
| 34 | - foreach ($definitions as $definition) { |
|
| 35 | + foreach ($definitions as $definition) |
|
| 36 | + { |
|
| 35 | 37 | $classes = $this->memory->loadData($definition->getHash()); |
| 36 | - if ($classes === null) { |
|
| 38 | + if ($classes === null) |
|
| 39 | + { |
|
| 37 | 40 | $this->memory->saveData( |
| 38 | 41 | $definition->getHash(), |
| 39 | 42 | $classes = $this->cacheBuilder->getClasses($definition) |
@@ -25,7 +25,8 @@ discard block |
||
| 25 | 25 | { |
| 26 | 26 | $listener = new \ReflectionClass($listener); |
| 27 | 27 | |
| 28 | - foreach ($this->reader->getClassMetadata($listener, ListenForClasses::class) as $attribute) { |
|
| 28 | + foreach ($this->reader->getClassMetadata($listener, ListenForClasses::class) as $attribute) |
|
| 29 | + { |
|
| 29 | 30 | // Analyze the target class from ListenForClasses attribute. |
| 30 | 31 | $refl = new \ReflectionClass($attribute->target); |
| 31 | 32 | |
@@ -33,7 +34,8 @@ discard block |
||
| 33 | 34 | $attr = $refl->getAttributes(\Attribute::class)[0] ?? null; |
| 34 | 35 | |
| 35 | 36 | // If the target class has no attribute, then it's a normal class or interface. |
| 36 | - if ($attr === null) { |
|
| 37 | + if ($attr === null) |
|
| 38 | + { |
|
| 37 | 39 | yield new ListenerDefinition( |
| 38 | 40 | listenerClass: $listener->getName(), |
| 39 | 41 | target: $refl, |