Completed
Pull Request — master (#48)
by
unknown
02:50
created

Package   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 5 1
A validate() 0 5 1
A create() 0 11 3
A isZipPresent() 0 6 2
1
<?php
2
3
namespace frictionlessdata\datapackage;
4
5
use Exception;
6
use ZipArchive;
7
8
class Package
9
{
10
11
  /**
12
   * @param $source
13
   * @param null $basePath
14
   *
15
   * @return \frictionlessdata\datapackage\Datapackages\BaseDatapackage
16
   * @throws \Exception
17
   * @throws \frictionlessdata\datapackage\Exceptions\DatapackageInvalidSourceException
18
   */
19
  public static function load($source, $basePath = null)
20
    {
21
        static::isZipPresent();
0 ignored issues
show
Bug introduced by
Since isZipPresent() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isZipPresent() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
22
        return Factory::datapackage($source, $basePath);
23
    }
24
25
  /**
26
   * @param $source
27
   * @param null $basePath
28
   *
29
   * @return \frictionlessdata\datapackage\Validators\DatapackageValidationError[]
30
   * @throws \Exception
31
   */
32
  public static function validate($source, $basePath = null)
33
    {
34
        static::isZipPresent();
0 ignored issues
show
Bug introduced by
Since isZipPresent() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isZipPresent() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
35
        return Factory::validate($source, $basePath);
36
    }
37
38
  /**
39
   * @param null $descriptor
40
   * @param null $basePath
41
   *
42
   * @return mixed
43
   * @throws \Exception
44
   */
45
  public static function create($descriptor = null, $basePath = null)
46
    {
47
        static::isZipPresent();
0 ignored issues
show
Bug introduced by
Since isZipPresent() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of isZipPresent() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
48
        $descriptor = Utils::objectify($descriptor);
49
        if ($descriptor && !isset($descriptor->resources)) {
50
            $descriptor->resources = [];
51
        }
52
        $packageClass = Factory::getDatapackageClass($descriptor);
53
54
        return new $packageClass($descriptor, $basePath, true);
55
    }
56
57
  /**
58
   * @throws \Exception
59
   */
60
  private static function isZipPresent() {
61
        //If ZipArchive is not available throw Exception.
62
        if (!class_exists('ZipArchive')) {
63
          throw new Exception('Error: Your PHP version is not compiled with zip support');
64
        }
65
    }
66
}
67