Completed
Push — master ( 766f26...9e4e10 )
by Elf
11s
created

Apps::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Apps;
4
5
use Illuminate\Contracts\Container\Container;
6
7
class Apps
8
{
9
    /**
10
     * The container instance.
11
     *
12
     * @var \Illuminate\Contracts\Container\Container
13
     */
14
    protected $container;
15
16
    /**
17
     * The current application identifier.
18
     *
19
     * @var string|null|false
20
     */
21
    protected $id = false;
22
23
    /**
24
     * Create a new Apps instance.
25
     *
26
     * @param  \Illuminate\Contracts\Container\Container  $container
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29 6
    public function __construct(Container $container)
30
    {
31 6
        $this->container = $container;
32
33 6
        $this->container->rebinding('request', function () {
34 1
            $this->refreshId();
35 6
        });
36 6
    }
37
38
    /**
39
     * Get or check the current application identifier.
40
     *
41
     * @return string|bool
42
     */
43 3
    public function id()
44
    {
45 3
        if ($this->id === false) {
46 3
            $this->id = $this->idForUrl($this->container['request']->getUri());
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->idForUrl($this->c...r['request']->getUri()) can also be of type integer. However, the property $id is declared as type string|null|false. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47 3
        }
48
49 3
        if (func_num_args() > 0) {
50 1
            return in_array($this->id, is_array(func_get_arg(0)) ? func_get_arg(0) : func_get_args());
51
        }
52
53 2
        return $this->id;
54
    }
55
56
    /**
57
     * Refresh the current application identifier.
58
     *
59
     * @return $this
60
     */
61 2
    public function refreshId()
62
    {
63 2
        $this->id = false;
64
65 2
        return $this;
66
    }
67
68
    /**
69
     * Get application identifier for the given URL.
70
     *
71
     * @param  string  $url
72
     * @return string
73
     */
74 4
    public function idForUrl($url)
75
    {
76 4
        $identifier = null;
77
78 4
        foreach ($this->container['config']['apps.url'] as $id => $root) {
79 4
            $root = preg_replace('~^https?://~i', '', $root);
80 4
            $pattern = '~^https?://'.preg_quote($root, '~').'([/\?#].*)?$~i';
81 4
            if (preg_match($pattern, $url)) {
82 4
                $len = strlen($root);
83 4
                if (! isset($length) || $length < $len) {
84 4
                    $length = $len;
85 4
                    $identifier = $id;
86 4
                }
87 4
            }
88 4
        }
89
90 4
        return $identifier;
91
    }
92
}
93