PackageItemFactory::fromModern()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 30
ccs 0
cts 26
cp 0
rs 8.439
c 1
b 0
f 0
cc 6
eloc 20
nc 9
nop 1
crap 42
1
<?php
2
3
namespace Buttress\Concrete\Service\Package;
4
5
use Loader;
6
use Package as LegacyPackage;
7
8
class PackageItemFactory
9
{
10
11
    /**
12
     * Get a package item from a modern package object
13
     *
14
     * @param \Concrete\Core\Package\Package $package
15
     * @return \Buttress\Concrete\Service\Package\PackageItem
16
     */
17
    public function fromModern($package)
18
    {
19
        if ($package instanceof \Concrete\Core\Entity\Package) {
0 ignored issues
show
Bug introduced by
The class Concrete\Core\Entity\Package does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
20
            $installed = true;
21
        } else {
22
            $installed = false;
23
24
            $method = new \ReflectionMethod($package, 'testForInstall');
25
            if ($method->isStatic()) {
26
                $installed = $package->isPackageInstalled();
27
                $errors = $package::testForInstall($package->getPackageHandle(), true);
28
            } else {
29
                $errors = $package->testForInstall(true);
30
            }
31
32
            if (is_array($errors)) {
33
                $installed = in_array($package::E_PACKAGE_INSTALLED, $errors, true);
34
            }
35
36
            if ($installed) {
37
                $errors = false;
38
            }
39
        }
40
41
        return (new PackageItem())
42
            ->setHandle($package->getPackageHandle())
43
            ->setVersion($package->getPackageVersion())
44
            ->setInstalled($errors ? false : $installed)
0 ignored issues
show
Bug introduced by
The variable $errors does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
45
            ->setInstalledVersion($package->getPackageVersion());
46
    }
47
48
    /**
49
     * Get a package item from a legacy package object
50
     * @param \Package $package
51
     * @return \Buttress\Concrete\Service\Package\PackageItem
52
     */
53
    public function fromLegacy(LegacyPackage $package)
54
    {
55
        $installedVersion = null;
56
        Loader::model('package');
57
        if ($installed = LegacyPackage::getByHandle($package->getPackageHandle())) {
58
            $package = $installed;
59
            $installedVersion = $this->getPackageVersionInstalled(Loader::db(), $package);
60
        }
61
62
        return (new PackageItem())
63
            ->setHandle($package->getPackageHandle())
64
            ->setVersion($package->getPackageVersion())
65
            ->setInstalled((bool) $package->isPackageInstalled())
66
            ->setInstalledVersion($installedVersion);
67
    }
68
69
    private function getPackageVersionInstalled(\ADOConnection $db, LegacyPackage $package)
70
    {
71
        return $db->GetOne('SELECT pkgVersion from Packages where pkgID=?', [$package->getPackageID()]);
72
    }
73
74
    /**
75
     * Create from raw values
76
     *
77
     * @param string $handle
78
     * @param string $version
79
     * @param bool $installed
80
     * @return \Buttress\Concrete\Service\Package\PackageItem
81
     */
82
    public function create($handle, $version, $installed)
83
    {
84
        return (new PackageItem())->setHandle($handle)->setVersion($version)->setInstalled($installed);
85
    }
86
87
}
88