findComposerPackageForClassName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Facade\Ignition\SolutionProviders;
4
5
use Facade\Ignition\Solutions\MissingPackageSolution;
6
use Facade\Ignition\Support\Packagist\Package;
7
use Facade\Ignition\Support\Packagist\Packagist;
8
use Facade\IgnitionContracts\HasSolutionsForThrowable;
9
use Illuminate\Support\Str;
10
use Throwable;
11
12
class MissingPackageSolutionProvider implements HasSolutionsForThrowable
13
{
14
    /** @var \Facade\Ignition\Support\Packagist\Package|null */
15
    protected $package;
16
17
    public function canSolve(Throwable $throwable): bool
18
    {
19
        $pattern = '/Class \'([^\s]+)\' not found/m';
20
21
        if (! preg_match($pattern, $throwable->getMessage(), $matches)) {
22
            return false;
23
        }
24
25
        $class = $matches[1];
26
27
        if (Str::startsWith($class, app()->getNamespace())) {
28
            return false;
29
        }
30
31
        $this->package = $this->findPackageFromClassName($class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->findPackageFromClassName($class) can also be of type object<Facade\Flare\Support\Packagist\Package>. However, the property $package is declared as type object<Facade\Ignition\S...Packagist\Package>|null. 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...
32
33
        return ! is_null($this->package);
34
    }
35
36
    public function getSolutions(Throwable $throwable): array
37
    {
38
        return [new MissingPackageSolution($this->package)];
0 ignored issues
show
Bug introduced by
It seems like $this->package can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
39
    }
40
41
    protected function findPackageFromClassName(string $missingClassName): ?Package
42
    {
43
        if (! $package = $this->findComposerPackageForClassName($missingClassName)) {
44
            return null;
45
        }
46
47
        return $package->hasNamespaceThatContainsClassName($missingClassName)
48
            ? $package
49
            : null;
50
    }
51
52
    protected function findComposerPackageForClassName(string $className): ?Package
53
    {
54
        $packages = Packagist::findPackagesForClassName($className);
55
56
        return $packages[0] ?? null;
57
    }
58
}
59