Completed
Pull Request — master (#24)
by yuuki
02:14
created

AbstractLogger::logFormatter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 2
eloc 13
nc 2
nop 2
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 Ray\Aop\MethodInvocation;
21
use Ytake\LaravelAspect\Annotation\LoggableAnnotate;
22
23
/**
24
 * Class AbstractLogger
25
 */
26
class AbstractLogger
27
{
28
    /** @var string */
29
    protected $format = "%s:%s.%s";
30
31
    /**
32
     * @param LoggableAnnotate $annotation
33
     * @param MethodInvocation $invocation
34
     *
35
     * @return string[]
36
     */
37
    protected function logFormatter(LoggableAnnotate $annotation, MethodInvocation $invocation)
38
    {
39
        $context = [];
40
        $arguments = $invocation->getArguments();
41
        foreach ($invocation->getMethod()->getParameters() as $parameter) {
42
            $context['args'][$parameter->name] = $arguments[$parameter->getPosition()];
43
        }
44
45
        return [
46
            '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...
47
            'message' => sprintf(
48
                $this->format,
49
                $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...
50
                $invocation->getMethod()->class,
51
                $invocation->getMethod()->name
52
            ),
53
            'context' => $context,
54
        ];
55
    }
56
}
57