Issues (134)

src/services/Cache.php (22 issues)

1
<?php
2
/**
3
 * FastCGI Cache Bust plugin for Craft CMS 3.x
4
 *
5
 * Bust the Nginx FastCGI Cache when entries are saved or created.
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\fastcgicachebust\services;
12
13
use Craft;
14
use craft\base\Component;
15
use craft\base\Element;
16
use craft\elements\Entry;
17
use craft\helpers\ElementHelper;
18
use craft\helpers\FileHelper;
19
use nystudio107\fastcgicachebust\FastcgiCacheBust;
20
use yii\base\ErrorException;
21
22
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
23
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
24
 * @package   FastcgiCacheBust
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
25
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
26
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
27
class Cache extends Component
28
{
29
    // Public Methods
30
    // =========================================================================
31
32
    /**
33
     * Clears the entirety of the FastCGI Cache
34
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
35
    public function clearAll(): void
36
    {
37
        $settings = FastcgiCacheBust::$plugin->getSettings();
0 ignored issues
show
The method getSettings() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        /** @scrutinizer ignore-call */ 
38
        $settings = FastcgiCacheBust::$plugin->getSettings();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
        if ($settings !== null && !empty($settings->cachePath)) {
39
            $cacheDirs = explode(',', $settings->cachePath);
40
            foreach ($cacheDirs as $cacheDir) {
41
                $cacheDir = Craft::parseEnv($cacheDir);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
                $cacheDir = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($cacheDir);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
                $cacheDir = trim($cacheDir);
0 ignored issues
show
It seems like $cacheDir can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                $cacheDir = trim(/** @scrutinizer ignore-type */ $cacheDir);
Loading history...
43
                try {
44
                    FileHelper::clearDirectory($cacheDir);
45
                } catch (ErrorException $errorException) {
46
                    Craft::error($errorException->getMessage(), __METHOD__);
47
                }
48
49
                Craft::info(
50
                    Craft::t(
51
                        'fastcgi-cache-bust',
52
                        'FastCGI Cache busted: `' . $cacheDir
53
                    ),
54
                    __METHOD__
55
                );
56
            }
57
        }
58
    }
59
60
    /**
61
     * Determine whether the cache should be busted or not based on the $element
62
     *
63
     * @param Element $element
0 ignored issues
show
Missing parameter comment
Loading history...
64
     *
65
     * @return bool
66
     */
67
    public function shouldBustCache(Element $element): bool
68
    {
69
        // Don't bust the cache if the element isn't ENABLED or LIVE
70
        if (($element->getStatus() !== Element::STATUS_ENABLED) && ($element->getStatus() !== Entry::STATUS_LIVE)) {
71
            return false;
72
        }
73
        // Don't bust the cache if the element is a draft or revision
74
        if (ElementHelper::isDraftOrRevision($element)) {
75
            return false;
76
        }
77
78
        return true;
79
    }
80
}
81