PluginAbstractEntity::setAnnotation()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Plugin;
14
15
use Apix\Entity;
16
17
abstract class PluginAbstractEntity extends PluginAbstract
18
{
19
    /**
20
     * The annotation key string for thsi plugin, e.g. "api_NAME".
21
     * @var string
22
     */
23
    protected $annotation;
24
25
    /**
26
     * Holds this plugin entity.
27
     * @var Entity
28
     */
29
    protected $entity;
30
31
    /**
32
     * Holds all the extracted subtags.
33
     * var array|null
34
     */
35
    private $subtags_extract = null;
36
37
    /**
38
     * Returns the values of the specified subtag.
39
     *
40
     * @param  string      $key     The subtag $key to retrieve
41
     * @param  string|null $default The default value
42
     * @return array|null  An indexed array of values, or null
43
     */
44 View Code Duplication
    public function getSubTagValues($key, array $default=null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
45
    {
46
        $tags = $this->extractSubTags();
47
        $k = array_search($key, $tags['keys']);
48
49
        // return false === $k
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
50
        //         ? $default
51
        //         : array_map('trim', explode(',', $tags['values'][$k]));
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
        return false === $k ? $default : explode(',', $tags['values'][$k]);
53
    }
54
55
    /**
56
     * Returns the boolean value of the specified subtag.
57
     *
58
     * @param  string       $key     The subtag $key to retrieve
59
     * @param  string|null  $default The default value
60
     * @return boolean|null
61
     */
62
    public function getSubTagBool($key, $default=null)
63
    {
64
        $tags = $this->extractSubTags();
65
        $k = array_search($key, $tags['keys']);
66
67
        $value = $k === false
68
                    ? ( $default ? (bool) $default : null)
69
                    : $tags['values'][$k];
70
71
        return null === $value
72
                    ? null
73
                    : filter_var($value, FILTER_VALIDATE_BOOLEAN);
74
    }
75
76
    /**
77
     * Returns the value of the specified subtag.
78
     *
79
     * @param  string      $key     The subtag $key to retrieve
80
     * @param  string|null $default The default value
81
     * @return string|null
82
     */
83 View Code Duplication
    public function getSubTagString($key, $default=null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
84
    {
85
        $tags = $this->extractSubTags();
86
        $k = array_search($key, $tags['keys']);
87
88
        return $k === false ? $default : (string) $tags['values'][$k];
89
    }
90
91
    /**
92
     * Extracts all the subtags.
93
     *
94
     * @return array An associative array
95
     */
96
    public function extractSubTags()
97
    {
98
        // if (null === $this->subtags_extract) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
            $lines = $this->entity->getAnnotationValue($this->annotation);
100
101
            if (!is_array($lines)) {
102
                $lines = array($lines);
103
            }
104
105
            foreach ($lines as $line) {
106
                preg_match_all(
107
                    '/(?P<keys>[^=\s]+)=(?P<values>[^=\s]+)/i',
108
                    $line,
109
                    $this->subtags_extract
110
                );
111
            }
112
        // }
113
        return $this->subtags_extract;
114
    }
115
116
    /**
117
     * Sets this plugin entity.
118
     *
119
     * @param  Entity $entity
120
     * @return void
121
     */
122
    public function setEntity(Entity $entity)
123
    {
124
        $this->entity = $entity;
125
    }
126
127
    /**
128
     * Sets this plugin entity.
129
     *
130
     * @return Entity
131
     */
132
    public function getEntity()
133
    {
134
        return $this->entity;
135
    }
136
137
    /**
138
     * Sets the annotation string for this plugin.
139
     *
140
     * @return void
141
     */
142
    public function setAnnotation($string)
143
    {
144
        $this->annotation = $string;
145
    }
146
147
}
148