ToStringTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A createToString() 0 23 3
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) 2018 Yuuki Takezawa
17
 */
18
19
namespace Ytake\Lom\Factory;
20
21
use PhpParser\Node\Stmt\ClassMethod;
22
use Ytake\Lom\Exception\TraitMethodCallException;
23
24
/**
25
 * Class ToStringTrait.
26
 *
27
 * @author yuuki.takezawa<[email protected]>
28
 */
29
trait ToStringTrait
30
{
31
    /**
32
     * @param \string[] $getters
33
     *
34
     * @return ClassMethod
35
     */
36
    protected function createToString(array $getters): ClassMethod
37
    {
38
        if (!$this instanceof AbstractDriver) {
39
            throw new TraitMethodCallException("should extend " . AbstractDriver::class);
40
        }
41
        /** @var \ReflectionClass $reflector */
42
        $reflector = $this->reflector;
0 ignored issues
show
Bug introduced by
The property reflector cannot be accessed from this context as it is declared protected in class Ytake\Lom\Factory\AbstractDriver.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
43
        $class = $reflector->getName();
0 ignored issues
show
Bug introduced by
Consider using $reflector->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
44
        $classMethods = [];
45
        foreach ($getters as $getter) {
46
            $classMethods[] = "\$this->{$getter['method']}()";
47
        }
48
        $stringBuilder = implode(" . ', ' . ", $classMethods);
49
        $build = "return '{$class}(' . $stringBuilder . ')';";
50
        /** @var \PhpParser\BuilderFactory $builder */
51
        $builder = $this->builder;
0 ignored issues
show
Bug introduced by
The property builder cannot be accessed from this context as it is declared protected in class Ytake\Lom\Factory\AbstractDriver.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
52
53
        return $builder->method('__toString')
54
            ->setDocComment('')
55
            ->addStmt(
56
                new \PhpParser\Node\Stmt\Class_($build)
57
            )->makePublic()->getNode();
58
    }
59
}
60