Cache   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
c 4
b 0
f 0
lcom 1
cbo 0
dl 0
loc 43
ccs 0
cts 18
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B writeCache() 0 25 4
1
<?php
2
3
namespace BrainExe\Expression;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Annotations\Annotations\Service;
7
use BrainExe\Core\Traits\FileCacheTrait;
8
9
/**
10
 * @Service("Expression.Cache", public=false)
11
 */
12
class Cache
13
{
14
15
    use FileCacheTrait;
16
    const CACHE_FILE = 'expressions';
17
18
    /**
19
     * @Inject({
20
     *  "@Expression.Gateway",
21
     * })
22
     * @param Gateway $gateway
23
     */
24
    public function __construct(Gateway $gateway)
25
    {
26
        $this->gateway = $gateway;
0 ignored issues
show
Bug introduced by
The property gateway does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
    }
28
29
    public function writeCache()
30
    {
31
        $all = $this->gateway->getAll();
32
33
        $content = "use \\BrainExe\\Core\\EventDispatcher\\AbstractEvent;\n\nreturn function(AbstractEvent \$event, \$eventName, \$listener) {\n";
34
        foreach ($all as $entity) {
35
            if (!$entity->compiledCondition) {
36
                continue;
37
            }
38
39
            if (!$entity->enabled) {
40
                continue;
41
            }
42
43
            $content .= sprintf(
44
                "\t\t\$entity = %s;\n\t\tif (%s) {\n\t\t\tyield \$entity;\n\t\t}\n",
45
                var_export($entity, true),
46
                $entity->compiledCondition
47
            );
48
        }
49
50
        $content .= "};";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal }; does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
51
52
        $this->dumpCacheFile(self::CACHE_FILE, $content);
53
    }
54
}
55