Completed
Push — develop ( 465be7...e649e8 )
by
unknown
15:38 queued 07:53
created

EntityTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 14
lcom 0
cbo 1
dl 0
loc 59
rs 10
c 3
b 1
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
C notEmpty() 0 33 8
B hasProperty() 0 22 6
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
/** */
11
namespace Core\Entity;
12
13
use Core\Entity\Exception\OutOfBoundsException;
14
15
/**
16
 * Implementation of \Core\Entity\EntityInterface.
17
 *
18
 *
19
 * @var EntityInterface $this
20
 *
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @since 0.25
23
 */
24
trait EntityTrait
25
{
26
    public function notEmpty($property, array $args=[])
0 ignored issues
show
Coding Style introduced by
function notEmpty() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
    {
28
        $method = "get$property";
29
30
        if (!method_exists($this, $method)) {
31
            throw new OutOfBoundsException("'$property' is not a valid property of '" . get_class($this) . "'");
32
        }
33
34
        $value = count($args)
35
            ? call_user_func_array([ $this, $method ], $args)
36
            : $this->$method();
37
38
        if (null === $value) { // is_scalar does not consider 'null' to be scalar value.
39
            return false;
40
        }
41
42
        if (is_scalar($value) || is_array($value)) {
43
            return !empty($value);
44
        }
45
46
        if (is_resource($value)) {
47
            return true;
48
        }
49
50
        /*
51
         * $value must be an object.
52
         */
53
        if ($value instanceOf \Countable) {
54
            return (bool) count($value);
55
        }
56
57
        return true;
58
    }
59
60
    public function hasProperty($property, $mode = self::PROPERTY_STRICT)
61
    {
62
        $hasProperty = property_exists($this, $property);
63
64
        if (!$hasProperty || self::PROPERTY_FACILE === $mode) {
65
            return $hasProperty;
66
        }
67
68
        $hasGetter = method_exists($this, "get$property");
69
70
        if (self::PROPERTY_GETTER === $mode) {
71
            return $hasGetter;
72
        }
73
74
        $hasSetter = method_exists($this, "set$property");
75
76
        if (self::PROPERTY_SETTER === $mode) {
77
            return $hasSetter;
78
        }
79
80
        return $hasGetter && $hasSetter;
81
    }
82
}
83