Completed
Push — 2.0 ( 4eaf55...2f0468 )
by Lukas Kahwe
10s
created

hasDirectContainerBuilderLogging()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 hasDirectContainerBuilderLogging(): bool
22
    {
23
        return method_exists('\Symfony\Component\DependencyInjection\ContainerBuilder', 'log');
24
    }
25
26
    /**
27
     * @return string
28
     */
29
    public static function getContainerResolvableRootWebPath(): string
30
    {
31
        return sprintf('%%kernel.project_dir%%/%s', self::isKernelLessThan(4) ? 'web' : 'public');
32
    }
33
34
    /**
35
     * @param int      $major
36
     * @param int|null $minor
37
     * @param int|null $patch
38
     *
39
     * @return bool
40
     */
41
    public static function isKernelGreaterThanOrEqualTo(int $major, int $minor = null, int $patch = null): bool
42
    {
43
        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...
44
    }
45
46
    /**
47
     * @param int      $major
48
     * @param int|null $minor
49
     * @param int|null $patch
50
     *
51
     * @return bool
52
     */
53
    public static function isKernelLessThan(int $major, int $minor = null, int $patch = null): bool
54
    {
55
        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...
56
    }
57
58
    /**
59
     * @param string   $operator
60
     * @param int      $major
61
     * @param int|null $minor
62
     * @param int|null $patch
63
     *
64
     * @return bool
65
     */
66
    private static function kernelVersionCompare(string $operator, int $major, int $minor = null, int $patch = null): bool
67
    {
68
        return version_compare(Kernel::VERSION_ID, sprintf("%d%'.02d%'.02d", $major, $minor ?: 0, $patch ?: 0), $operator);
69
    }
70
}
71