Completed
Push — master ( 5d4483...359550 )
by yuuki
01:38
created

AnnotateClass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A getPostConstructMethod() 0 15 5
1
<?php
2
declare(strict_types=1);
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 *
12
 * This software consists of voluntary contributions made by many individuals
13
 * and is licensed under the MIT license.
14
 *
15
 * Copyright (c) 2015-2018 Yuuki Takezawa
16
 *
17
 */
18
19
namespace Ytake\LaravelAspect;
20
21
use Ray\Aop\WeavedInterface;
22
use Ytake\LaravelAspect\Annotation\PostConstruct;
23
use function is_array;
24
use function unserialize;
25
26
/**
27
 * Class AnnotateClass
28
 */
29
final class AnnotateClass
30
{
31
    /**
32
     * @param WeavedInterface $weavedInstance
33
     *
34
     * @return string
35
     */
36
    public function getPostConstructMethod(WeavedInterface $weavedInstance): string
37
    {
38
        $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...
39
        if (!is_array($methods)) {
40
            return '';
41
        }
42
        foreach ($methods as $method => $annotations) {
43
            foreach ($annotations as $annotation) {
44
                if ($annotation instanceof PostConstruct) {
45
                    return $method;
46
                }
47
            }
48
        }
49
        return '';
50
    }
51
}
52