Completed
Push — master ( 203a68...d4d7bc )
by yuuki
15s queued 10s
created

AbstractLogger   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 3
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A logFormatter() 0 19 2
A setLogger() 0 4 1
1
<?php
2
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-2016 Yuuki Takezawa
16
 *
17
 */
18
namespace Ytake\LaravelAspect\Interceptor;
19
20
use Psr\Log\LoggerInterface;
21
use Ray\Aop\MethodInvocation;
22
use Ytake\LaravelAspect\Annotation\LoggableAnnotate;
23
24
/**
25
 * Class AbstractLogger
26
 */
27
class AbstractLogger
28
{
29
    /** @var string */
30
    protected $format = "%s:%s.%s";
31
32
    /** @var LoggerInterface  */
33
    protected static $logger;
34
35
    /**
36
     * @param LoggableAnnotate $annotation
37
     * @param MethodInvocation $invocation
38
     *
39
     * @return string[]
40
     */
41
    protected function logFormatter(LoggableAnnotate $annotation, MethodInvocation $invocation)
42
    {
43
        $context = [];
44
        $arguments = $invocation->getArguments();
45
        foreach ($invocation->getMethod()->getParameters() as $parameter) {
46
            $context['args'][$parameter->name] = $arguments[$parameter->getPosition()];
47
        }
48
49
        return [
50
            'level' => $annotation->value,
0 ignored issues
show
Bug introduced by
Accessing value on the interface Ytake\LaravelAspect\Annotation\LoggableAnnotate 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...
51
            'message' => sprintf(
52
                $this->format,
53
                $annotation->name,
0 ignored issues
show
Bug introduced by
Accessing name on the interface Ytake\LaravelAspect\Annotation\LoggableAnnotate 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...
54
                $invocation->getMethod()->class,
55
                $invocation->getMethod()->name
56
            ),
57
            'context' => $context,
58
        ];
59
    }
60
61
    /**
62
     * @param LoggerInterface $logger
63
     */
64
    public function setLogger(LoggerInterface $logger)
65
    {
66
        self::$logger = $logger;
67
    }
68
}
69