Completed
Push — develop ( c05732...6c11b8 )
by Abdelrahman
01:50
created

Bindable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bindRepository() 0 8 4
A prepareRepositoryAlias() 0 9 3
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Repository Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Repository Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Repository\Traits;
17
18
trait Bindable
19
{
20
    /**
21
     * The repository alias pattern.
22
     *
23
     * @var string
24
     */
25
    protected $repositoryAliasPattern = '{{class}}Contract';
26
27
    /**
28
     * Register an IoC binding whether it's already been registered or not.
29
     *
30
     * @param string               $abstract
31
     * @param \Closure|string|null $concrete
32
     * @param bool                 $shared
33
     * @param string|null          $alias
34
     * @param bool                 $force
35
     *
36
     * @return void
37
     */
38
    protected function bindRepository($abstract, $concrete = null, $shared = true, $alias = null, $force = false)
39
    {
40
        if (! $this->app->bound($abstract) || $force) {
41
            $concrete = $concrete ?: $abstract;
42
            $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...
43
            $this->app->alias($abstract, $this->prepareRepositoryAlias($alias, $concrete));
44
        }
45
    }
46
47
    /**
48
     * Prepare the repository alias.
49
     *
50
     * @param string|null $alias
51
     * @param mixed       $concrete
52
     *
53
     * @return string
54
     */
55
    protected function prepareRepositoryAlias($alias, $concrete)
56
    {
57
        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...
58
            $concrete = str_replace('Repositories', 'Contracts', $concrete);
59
            $alias = str_replace('{{class}}', $concrete, $this->repositoryAliasPattern);
60
        }
61
62
        return $alias;
63
    }
64
}
65