Completed
Push — master ( 69b436...e84470 )
by Kirill
14:54 queued 05:27
created

AliasProvider::createAlias()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Query;
11
12
use Illuminate\Support\Str;
13
use RDS\Hydrogen\Query;
14
15
/**
16
 * Trait AliasProvider
17
 * @mixin Query
18
 */
19
trait AliasProvider
20
{
21
    /**
22
     * @var int
23
     */
24
    protected static $lastQueryId = 0;
25
26
    /**
27
     * @var string|null
28
     */
29
    protected $alias;
30
31
    /**
32
     * @return string|null
33
     */
34 32
    public function getAlias(): ?string
35
    {
36 32
        if ($this->alias === null) {
37 32
            $this->alias = $this->repository
0 ignored issues
show
Bug introduced by
The property repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38 29
                ? $this->createAlias($this->getRepository()->getClassName())
0 ignored issues
show
Bug introduced by
It seems like getRepository() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39 3
                : $this->createAlias();
40
        }
41
42 32
        return $this->alias;
43
    }
44
45
    /**
46
     * @param string|null $pattern
47
     * @return string
48
     */
49 32
    private function createAlias(string $pattern = null): string
50
    {
51 32
        $name = $pattern
52 29
            ? \snake_case(\class_basename($pattern))
53 32
            : 'q' . Str::random(7);
54
55 32
        return \sprintf('%s_%d', $name, ++static::$lastQueryId);
56
    }
57
58
    /**
59
     * @param string|null $pattern
60
     * @return string
61
     */
62 8
    public function placeholder(string $pattern = null): string
63
    {
64 8
        return ':' . $this->createAlias($pattern);
65
    }
66
}
67