Ajde_Model_Revision::purgeRevisions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
abstract class Ajde_Model_Revision extends Ajde_Model
4
{
5
    protected $_ignoreFieldInRevision = [];
6
    protected $_ignoreFieldInRevisionIfEmpty = [];
7
8
    const DEFAULT_LIMIT = 50;
9
10
    public function getRevisionsHtml($crud = null)
11
    {
12
        if (!$this->getPK()) {
13
            return;
14
        }
15
16
        $revisions = new RevisionCollection();
17
        $revisions->addFilter(new Ajde_Filter_Where('model', Ajde_Filter::FILTER_EQUALS, $this->getModelName()));
18
        $revisions->addFilter(new Ajde_Filter_Where('foreignkey', Ajde_Filter::FILTER_EQUALS, $this->getPK()));
19
        $revisions->orderBy('time', 'DESC');
20
        $revisions->limit(self::DEFAULT_LIMIT);
21
22
        $controller = Ajde_Controller::fromRoute(new Ajde_Core_Route('_core/crud:revisions'));
23
        $controller->setRevisions($revisions);
0 ignored issues
show
Documentation Bug introduced by
The method setRevisions does not exist on object<Ajde_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
24
        $controller->setModel($this);
0 ignored issues
show
Documentation Bug introduced by
The method setModel does not exist on object<Ajde_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
25
        $controller->setCrudInstance($crud);
0 ignored issues
show
Documentation Bug introduced by
The method setCrudInstance does not exist on object<Ajde_Controller>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
26
27
        return $controller->invoke();
28
    }
29
30
    private function getModelName()
31
    {
32
        $modelNameCC = str_replace('Model', '', get_class($this));
33
34
        return $this->_tableName = $this->fromCamelCase($modelNameCC);
35
    }
36
37
    public function purgeRevisions()
38
    {
39
        if (!$this->getPK()) {
40
            return false;
41
        }
42
43
        $revisions = new RevisionCollection();
44
        $revisions->addFilter(new Ajde_Filter_Where('model', Ajde_Filter::FILTER_EQUALS, $this->getModelName()));
45
        $revisions->addFilter(new Ajde_Filter_Where('foreignkey', Ajde_Filter::FILTER_EQUALS, $this->getPK()));
46
        $revisions->deleteAll();
47
48
        return true;
49
    }
50
51
    public function save()
52
    {
53
        // check all changed fields
54
        $modelName = get_class($this);
55
        $shadowModel = new $modelName();
56
        /* @var $shadowModel Ajde_Model */
57
        $shadowModel->loadByPK($this->getPK());
58
        if ($shadowModel->_hasMeta) {
59
            $shadowModel->populateMeta();
60
        }
61
62
        // old values
63
        $oldValues = $shadowModel->values();
64
        foreach ($oldValues as &$oldValue) {
65
            @$oldValue = (string) $oldValue;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
66
        }
67
68
        // populate meta of current model, but don't override
69
        if ($this->_hasMeta) {
70
            $this->populateMeta(false, false);
71
        }
72
73
        // new values
74
        $newValues = $this->values();
75
        foreach ($newValues as $k => &$newValue) {
76
            if ($k == 'meta_4') {
77
                //                die('hier');
78
            }
79
            @$newValue = (string) $newValue;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
80
        }
81
82
        // ignore fields
83
        foreach ($this->_ignoreFieldInRevision as $ignoreField) {
84
            unset($oldValues[$ignoreField]);
85
            unset($newValues[$ignoreField]);
86
        }
87
88
        // ignore fields
89
        foreach ($this->_ignoreFieldInRevisionIfEmpty as $ignoreField) {
90
            if (!isset($newValues[$ignoreField]) || empty($newValues[$ignoreField])) {
91
                unset($oldValues[$ignoreField]);
92
                unset($newValues[$ignoreField]);
93
            }
94
        }
95
96
        if ($diffs = array_diff_assoc($oldValues, $newValues)) {
97
            foreach ($diffs as $diffField => $diffValue) {
98
                $revision = new RevisionModel();
99
                $revision->model = $this->getModelName();
0 ignored issues
show
Documentation introduced by
The property model does not exist on object<RevisionModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
100
                $revision->foreignkey = $this->getPK();
0 ignored issues
show
Documentation introduced by
The property foreignkey does not exist on object<RevisionModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
                $revision->user = UserModel::getLoggedIn();
0 ignored issues
show
Documentation introduced by
The property user does not exist on object<RevisionModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
                $revision->field = $diffField;
0 ignored issues
show
Documentation introduced by
The property field does not exist on object<RevisionModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
                $revision->old = issetor($oldValues[$diffField]);
0 ignored issues
show
Documentation introduced by
The property old does not exist on object<RevisionModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
Are you sure the assignment to $revision->old is correct as issetor($oldValues[$diffField]) (which targets issetor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
104
                $revision->new = issetor($newValues[$diffField]);
0 ignored issues
show
Documentation introduced by
The property new does not exist on object<RevisionModel>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Bug introduced by
Are you sure the assignment to $revision->new is correct as issetor($newValues[$diffField]) (which targets issetor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
105
                $revision->insert();
106
            }
107
        }
108
109
        return parent::save();
110
    }
111
}
112