Completed
Push — master ( 6e1671...19b3e1 )
by
unknown
14:18
created

LocalUpgradePackage::setExtractedPackagePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Data\Provider;
4
5
use Symfony\Component\Finder\Finder;
6
7
use Symfony\Component\Filesystem\Filesystem;
8
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
9
use ZipArchive;
10
11
12
class LocalUpgradePackage implements PackageDataProviderInterface
13
{
14
    private $upgradePackagePath;
0 ignored issues
show
Unused Code introduced by
The property $upgradePackagePath is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    private $zipExtractor;
0 ignored issues
show
Unused Code introduced by
The property $zipExtractor is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    private $data;
0 ignored issues
show
Unused Code introduced by
The property $data is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
20
    private $etractedPackagePath;
21
22
23
    /**
24
     * LocalUpgradePackage constructor.
25
     *
26
     * @param Cache         $cache
0 ignored issues
show
Bug introduced by
There is no parameter named $cache. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
27
     * @param $zipExtractor
28
     * @param String $pathToBuild
0 ignored issues
show
Bug introduced by
There is no parameter named $pathToBuild. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
29
     */
30
    public function __construct()
31
    {
32
33
    }
34
35
    /*
36
     * calculates all modified files in new directory
37
     */
38
    public function getListOfCustomizations(Context $context)
39
    {
40
41
        if ($context->pathToUpgradePackage) {
42
            $this->setExtractedPackagePath($context->version);
43
            $this->initPackageDirectory($this->getExtractedPackagePath());
44
            $this->extractUpgradePackage($context->pathToUpgradePackage);
45
            $customizations = array();
46
            $finder = new Finder();
47
            $customizations['deleted_files'] =
48
                $this->getDirectoryFileList($finder, $this->etractedPackagePath . '/scripts/files_to_remove/');
49
50
            $customizations['sql_scripts'] =
51
                $this->getDirectoryFileList($finder, $this->etractedPackagePath . '/scripts/');
52
53
            $customizations['sql_scripts'] =
54
                $this->getDirectoryFileList($finder, $this->etractedPackagePath . '/scripts/');
55
        }
56
        return $customizations;
0 ignored issues
show
Bug introduced by
The variable $customizations 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...
57
    }
58
    /**
59
     * Get list of files needs to be deleted
60
     *
61
     * @param String $pathToUpgradePackage
62
     *
63
     * @return mixed
64
     */
65
    public function getListOfDeletedFiles($pathToUpgradePackage)
0 ignored issues
show
Unused Code introduced by
The parameter $pathToUpgradePackage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        return '';
68
    }
69
    /**
70
     * Get list of files needs to be Modified
71
     *
72
     * @param String $pathToUpgradePackage
73
     *
74
     * @return mixed
75
     */
76
    public function getListOfModifiedFiles($pathToUpgradePackage)
0 ignored issues
show
Unused Code introduced by
The parameter $pathToUpgradePackage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
77
    {
78
        return '';
79
    }
80
    /**
81
     * Get list of scripts needs to be run for upgrade
82
     *
83
     * @param String $pathToUpgradePackage
84
     *
85
     * @return mixed
86
     */
87
    public function getListOfScripts($pathToUpgradePackage)
0 ignored issues
show
Unused Code introduced by
The parameter $pathToUpgradePackage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
        return '';
90
    }
91
    /**
92
     * Get list of sql migration scripts needs to be run for upgrade
93
     *
94
     * @param String $pathToUpgradePackage
95
     *
96
     * @return mixed
97
     */
98
    public function getListOfSQLScripts($pathToUpgradePackage)
0 ignored issues
show
Unused Code introduced by
The parameter $pathToUpgradePackage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
    {
100
        return '';
101
    }
102
103
104
    /*
105
     * @param Finder $finder
106
     * @param String $path
107
     * return mixed
108
     */
109
    private function getDirectoryFileList($finder, $path)
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
        $filesLIst = [];
112
        $finder->files()->in($this->pathToUpgradePackage . '/scripts/files_to_remove/');
0 ignored issues
show
Bug introduced by
The property pathToUpgradePackage 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...
113
        foreach ($finder as $file) {
114
            $filesLIst[] = $file->getRelativePathname();
115
        }
116
        return $filesLIst;
117
    }
118
119
    /*
120
     * @param Finder $finder
121
     * @param String $installedBuildPath
122
     * @param String $upgradePackagePath
0 ignored issues
show
Bug introduced by
There is no parameter named $upgradePackagePath. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
123
     * return mixed
124
     */
125
    private function getNewOrChangedFiles($finder, $installedBuildPath, $needstoBeUpgradedPath)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
126
    {
127
        $filesLIst = [];
128
        $finder->files()->in($needstoBeUpgradedPath);
129
        foreach ($finder as $file) {
130
            $fileName = $file->getRelativePathname();
131
            if (!file_exists($installedBuildPath . $fileName))
132
                $filesLIst[] = $fileName;
133
        }
134
        return $filesLIst;
135
    }
136
137
    /*
138
     * Extracts zipped upgrade package
139
     * @param String $pathToZipPackage
0 ignored issues
show
Bug introduced by
There is no parameter named $pathToZipPackage. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
140
     *
141
     */
142
    private function extractUpgradePackage($pathToUpgradePackage)
143
    {
144
        $unzip = new ZipArchive;
145
        $out = $unzip->open($pathToUpgradePackage);
146
147
        if ($out === TRUE) {
148
            $unzip->extractTo($this->getExtractedPackagePath());
149
            $unzip->close();
150
            echo 'Upgrade Package unzipped successfully' . "\n";
151
        } else {
152
            echo 'Something went wrong?';
153
        }
154
    }
155
156
157
158
    /**
159
     * @return string
160
     */
161
    protected function getExtractedPackagePath()
162
    {
163
        return $this->etractedPackagePath;
164
    }
165
166
    /**
167
     * @param string $upgradePackagePath
0 ignored issues
show
Bug introduced by
There is no parameter named $upgradePackagePath. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
168
     * @param string version
169
     */
170
    public function setExtractedPackagePath($version)
171
    {
172
        $this->etractedPackagePath = '/tmp/' . $version . '/';
173
    }
174
175
176
    /**
177
     * clear packege for unzipped package
178
     */
179
    private function clearUpgradePackageDirectory($extractedPackagePath)
180
    {
181
        $fs = new Filesystem();
182
        $fs->remove($extractedPackagePath);
183
    }
184
185
    /**
186
     * Creates directory for extracting zip package
187
     * @return bool
188
     */
189
    private function initPackageDirectory($extractedPackagePath)
190
    {
191
192
        $fs = new Filesystem();
193
194
        if ($fs->exists($extractedPackagePath))
195
        {
196
            $this->clearUpgradePackageDirectory($extractedPackagePath);
197
            return true;
198
        }
199
200
        try {
201
            $fs->mkdir($extractedPackagePath);
202
            return true;
203
        } catch (IOExceptionInterface $e) {
204
            echo "An error occurred while creating upgrade package directory ".$e->getPath();
205
        }
206
207
    }
208
209
}
210