Failed Conditions
Push — dev/plugin-misc ( f22d99...066a4d )
by Kiyotaka
10:54 queued 03:43
created

CacheUtil::clearDoctrineCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Util;
15
16
use Symfony\Bundle\FrameworkBundle\Console\Application;
17
use Symfony\Component\Console\Input\ArrayInput;
18
use Symfony\Component\Console\Output\BufferedOutput;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\Finder\Finder;
23
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
24
use Symfony\Component\HttpKernel\KernelEvents;
25
use Symfony\Component\HttpKernel\KernelInterface;
26
27
/**
28
 * キャッシュ関連のユーティリティクラス.
29
 */
30
class CacheUtil implements EventSubscriberInterface
31
{
32
    private $clearCacheAfterResponse = false;
33
34
    /**
35
     * @var KernelInterface
36
     */
37
    protected $kernel;
38
39
    /**
40
     * CacheUtil constructor.
41
     *
42
     * @param KernelInterface $kernel
43
     */
44
    public function __construct(KernelInterface $kernel)
45
    {
46
        $this->kernel = $kernel;
47
    }
48
49
    /**
50
     * @param string $env
51
     */
52
    public function clearCache($env = null)
53
    {
54
        $this->clearCacheAfterResponse = $env;
0 ignored issues
show
Documentation Bug introduced by
It seems like $env can also be of type string. However, the property $clearCacheAfterResponse is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55
    }
56
57
    public function forceClearCache(PostResponseEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event 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...
58
    {
59
        if ($this->clearCacheAfterResponse === false) {
60
            return;
61
        }
62
63
        $console = new Application($this->kernel);
64
        $console->setAutoExit(false);
65
66
        $command = [
67
            'command' => 'cache:clear',
68
            '--no-warmup' => true,
69
            '--no-ansi' => true,
70
        ];
71
72
        if ($this->clearCacheAfterResponse !== null) {
73
            $command['--env'] = $this->clearCacheAfterResponse;
74
        }
75
76
        $input = new ArrayInput($command);
77
78
        $output = new BufferedOutput(
79
            OutputInterface::VERBOSITY_DEBUG,
80
            true
81
        );
82
83
        $console->run($input, $output);
84
85
        if (function_exists('opcache_reset')) {
86
            opcache_reset();
87
        }
88
89
        if (function_exists('apc_clear_cache')) {
90
            apc_clear_cache('user');
91
            apc_clear_cache();
92
        }
93
94
        if (function_exists('wincache_ucache_clear')) {
95
            wincache_ucache_clear();
96
        }
97
98
        return $output->fetch();
99
    }
100
101
    /**
102
     * Doctrineのキャッシュを削除します.
103
     * APP_ENV=prodの場合のみ実行されます.
104
     *
105
     * @param null $env
0 ignored issues
show
Bug introduced by
There is no parameter named $env. 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...
106
     * @return string
107
     * @throws \Exception
108
     */
109
    public function clearDoctrineCache()
110
    {
111
        if ($this->kernel->getEnvironment() !== 'prod') {
112
            return;
113
        }
114
        $console = new Application($this->kernel);
115
        $console->setAutoExit(false);
116
117
        $command = [
118
            'command' => 'cache:pool:clear',
119
            'pools' => ['doctrine.app_cache_pool'],
120
            '--no-ansi' => true,
121
        ];
122
123
        $input = new ArrayInput($command);
124
125
        $output = new BufferedOutput(
126
            OutputInterface::VERBOSITY_DEBUG,
127
            true
128
        );
129
130
        $console->run($input, $output);
131
132
        return $output->fetch();
133
    }
134
135
    /**
136
     * Twigキャッシュを削除します.
137
     */
138
    public function clearTwigCache()
139
    {
140
        $cacheDir = $this->kernel->getCacheDir().'/twig';
141
        $fs = new Filesystem();
142
        $fs->remove($cacheDir);
143
    }
144
145
    /**
146
     * キャッシュを削除する.
147
     *
148
     * doctrine, profiler, twig によって生成されたキャッシュディレクトリを削除する.
149
     * キャッシュは $app['config']['root_dir'].'/app/cache' に生成されます.
150
     *
151
     * @param Application $app
152
     * @param boolean $isAll .gitkeep を残してすべてのファイル・ディレクトリを削除する場合 true, 各ディレクトリのみを削除する場合 false
153
     * @param boolean $isTwig Twigキャッシュファイルのみ削除する場合 true
154
     *
155
     * @return boolean 削除に成功した場合 true
156
     *
157
     * @deprecated CacheUtil::clearCacheを利用すること
158
     */
159
    public static function clear($app, $isAll, $isTwig = false)
160
    {
161
        $cacheDir = $app['config']['root_dir'].'/app/cache';
162
163
        $filesystem = new Filesystem();
164
        $finder = Finder::create()->notName('.gitkeep')->files();
165
        if ($isAll) {
166
            $finder = $finder->in($cacheDir);
167
            $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
168 View Code Duplication
        } elseif ($isTwig) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            if (is_dir($cacheDir.'/twig')) {
170
                $finder = $finder->in($cacheDir.'/twig');
171
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
172
            }
173
        } else {
174 View Code Duplication
            if (is_dir($cacheDir.'/doctrine')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
                $finder = $finder->in($cacheDir.'/doctrine');
176
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177
            }
178 View Code Duplication
            if (is_dir($cacheDir.'/profiler')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
                $finder = $finder->in($cacheDir.'/profiler');
180
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
181
            }
182 View Code Duplication
            if (is_dir($cacheDir.'/twig')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
                $finder = $finder->in($cacheDir.'/twig');
184
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
            }
186 View Code Duplication
            if (is_dir($cacheDir.'/translator')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
                $finder = $finder->in($cacheDir.'/translator');
188
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
            }
190
        }
191
192
        if (function_exists('opcache_reset')) {
193
            opcache_reset();
194
        }
195
196
        if (function_exists('apc_clear_cache')) {
197
            apc_clear_cache('user');
198
            apc_clear_cache();
199
        }
200
201
        if (function_exists('wincache_ucache_clear')) {
202
            wincache_ucache_clear();
203
        }
204
205
        return true;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public static function getSubscribedEvents()
212
    {
213
        return [KernelEvents::TERMINATE => 'forceClearCache'];
214
    }
215
}
216