Bindable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bindRepository() 0 8 4
A prepareRepositoryAlias() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Repository\Traits;
6
7
trait Bindable
8
{
9
    /**
10
     * The repository alias pattern.
11
     *
12
     * @var string
13
     */
14
    protected $repositoryAliasPattern = '{{class}}Contract';
15
16
    /**
17
     * Register an IoC binding whether it's already been registered or not.
18
     *
19
     * @param string               $abstract
20
     * @param \Closure|string|null $concrete
21
     * @param bool                 $shared
22
     * @param string|null          $alias
23
     * @param bool                 $force
24
     *
25
     * @return void
26
     */
27
    protected function bindRepository($abstract, $concrete = null, $shared = true, $alias = null, $force = false): void
28
    {
29
        if (! $this->app->bound($abstract) || $force) {
30
            $concrete = $concrete ?: $abstract;
31
            $this->app->bind($abstract, $concrete, $shared);
0 ignored issues
show
Bug introduced by
The property app 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...
32
            $this->app->alias($abstract, $this->prepareRepositoryAlias($alias, $concrete));
33
        }
34
    }
35
36
    /**
37
     * Prepare the repository alias.
38
     *
39
     * @param string|null $alias
40
     * @param mixed       $concrete
41
     *
42
     * @return string
43
     */
44
    protected function prepareRepositoryAlias($alias, $concrete): string
45
    {
46
        if (! $alias && ! $concrete instanceof \Closure) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
47
            $concrete = str_replace('Repositories', 'Contracts', $concrete);
48
            $alias = str_replace('{{class}}', $concrete, $this->repositoryAliasPattern);
49
        }
50
51
        return $alias;
52
    }
53
}
54