AnnotateClass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getPostConstructMethod() 0 16 5
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2020 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelAspect;
21
22
use Ray\Aop\WeavedInterface;
23
use Ytake\LaravelAspect\Annotation\PostConstruct;
24
25
use function is_array;
26
use function unserialize;
27
28
/**
29
 * Class AnnotateClass
30
 */
31
final class AnnotateClass
32
{
33
    /**
34
     * @param WeavedInterface $weavedInstance
35
     *
36
     * @return string
37
     */
38
    public function getPostConstructMethod(WeavedInterface $weavedInstance): string
39
    {
40
        $methods = unserialize($weavedInstance->methodAnnotations);
0 ignored issues
show
Bug introduced by
Accessing methodAnnotations on the interface Ray\Aop\WeavedInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
41
        if (!is_array($methods)) {
42
            return '';
43
        }
44
        foreach ($methods as $method => $annotations) {
45
            foreach ($annotations as $annotation) {
46
                if ($annotation instanceof PostConstruct) {
47
                    return $method;
48
                }
49
            }
50
        }
51
52
        return '';
53
    }
54
}
55