Completed
Push — 1.0 ( 30aea4...7767b3 )
by Rob
9s
created

SymfonyFramework::isKernelLessThan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Utility\Framework;
13
14
use Symfony\Component\HttpKernel\Kernel;
15
16
class SymfonyFramework
17
{
18
    /**
19
     * @return bool
20
     */
21
    public static function hasDefinitionSharing()
22
    {
23
        return method_exists('\Symfony\Component\DependencyInjection\Definition', 'setShared')
24
            && method_exists('\Symfony\Component\DependencyInjection\Definition', 'isShared');
25
    }
26
27
    /**
28
     * @return bool
29
     */
30
    public static function hasDefinitionScoping()
31
    {
32
        return method_exists('\Symfony\Component\DependencyInjection\Definition', 'setScope')
33
            && method_exists('\Symfony\Component\DependencyInjection\Definition', 'getScope');
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public static function hasDirectContainerBuilderLogging()
40
    {
41
        return method_exists('\Symfony\Component\DependencyInjection\ContainerBuilder', 'log');
42
    }
43
44
    /**
45
     * @param int      $major
46
     * @param int|null $minor
47
     * @param int|null $patch
48
     *
49
     * @return bool
50
     */
51
    public static function isKernelGreaterThanOrEqualTo($major, $minor = null, $patch = null)
52
    {
53
        return static::kernelVersionCompare('>=', $major, $minor, $patch);
0 ignored issues
show
Bug introduced by
Since kernelVersionCompare() 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 kernelVersionCompare() 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...
54
    }
55
56
    /**
57
     * @param int      $major
58
     * @param int|null $minor
59
     * @param int|null $patch
60
     *
61
     * @return bool
62
     */
63
    public static function isKernelLessThan($major, $minor = null, $patch = null)
64
    {
65
        return static::kernelVersionCompare('<', $major, $minor, $patch);
0 ignored issues
show
Bug introduced by
Since kernelVersionCompare() 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 kernelVersionCompare() 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...
66
    }
67
68
    /**
69
     * @param string   $operator
70
     * @param int      $major
71
     * @param int|null $minor
72
     * @param int|null $patch
73
     *
74
     * @return bool
75
     */
76
    private static function kernelVersionCompare($operator, $major, $minor = null, $patch = null)
77
    {
78
        $vernum = $major;
79
        $kernel = Kernel::MAJOR_VERSION;
80
81
        if ($minor) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $minor of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
82
            $vernum .= '.'.$minor;
83
            $kernel .= '.'.Kernel::MINOR_VERSION;
84
85
            if ($patch) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $patch of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
86
                $vernum .= '.'.$patch;
87
                $kernel .= '.'.Kernel::RELEASE_VERSION;
88
            }
89
        }
90
91
        return version_compare($kernel, $vernum, $operator);
92
    }
93
}
94