ContainerInterceptor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A intercept() 0 24 4
A resolveContextualBindings() 0 8 2
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 Illuminate\Contracts\Container\Container;
23
use Ray\Aop\BindInterface;
24
use Ray\Aop\WeavedInterface;
25
26
/**
27
 * Class ContainerInterceptor
28
 */
29
final class ContainerInterceptor
30
{
31
    /** @var Container|\Illuminate\Container\Container */
32
    private $container;
33
34
    /** @var AnnotateClass */
35
    private $annotateClass;
36
37
    /**
38
     * @param Container     $container
39
     * @param AnnotateClass $annotateClass
40
     */
41
    public function __construct(Container $container, AnnotateClass $annotateClass)
42
    {
43
        $this->container = $container;
44
        $this->annotateClass = $annotateClass;
45
    }
46
47
    /**
48
     * @param string $abstract
49
     * @param BindInterface   $bind
50
     * @param string $className
51
     *
52
     * @return bool
53
     */
54
    public function intercept(string $abstract, BindInterface $bind, string $className): bool
55
    {
56
        if ($abstract === $className) {
57
            return false;
58
        }
59
60
        if (isset($this->container->contextual[$abstract])) {
0 ignored issues
show
Bug introduced by
Accessing contextual on the interface Illuminate\Contracts\Container\Container 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...
61
            $this->resolveContextualBindings($abstract, $className);
62
        }
63
        $this->container->bind($abstract, function (Container $app, array $params = []) use ($bind, $className) {
64
            /** @var WeavedInterface $instance */
65
            $instance = $app->make($className, $params);
66
            $instance->bindings = $bind->getBindings();
0 ignored issues
show
Bug introduced by
Accessing bindings 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...
67
            $method = $this->annotateClass->getPostConstructMethod($instance);
68
            if (!empty($method)) {
69
                $instance->bindings = $bind->getBindings();
0 ignored issues
show
Bug introduced by
Accessing bindings 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...
70
                $instance->$method();
71
            }
72
73
            return $instance;
74
        });
75
76
        return true;
77
    }
78
79
    /**
80
     * @param string $class
81
     * @param string $compiledClass
82
     */
83
    private function resolveContextualBindings(string $class, string $compiledClass): void
84
    {
85
        foreach ($this->container->contextual[$class] as $abstract => $concrete) {
0 ignored issues
show
Bug introduced by
Accessing contextual on the interface Illuminate\Contracts\Container\Container 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...
86
            $this->container->when($compiledClass)
87
                ->needs($abstract)
88
                ->give($concrete);
89
        }
90
    }
91
}
92