Test Failed
Pull Request — master (#20)
by
unknown
04:23
created

AbstractCustomFieldsModel::getAllCustomFields()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 49
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.5923

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 5
nop 2
dl 0
loc 49
ccs 12
cts 18
cp 0.6667
crap 4.5923
rs 9.7
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Gewaer\CustomFields;
5
use Exception;
6
use Gewaer\Models\Modules;
7
8
abstract class AbstractCustomFieldsModel extends \Baka\Database\ModelCustomFields
9
{
10
    /**
11
     * Get all custom fields of the given object
12
     *
13
     * @param  array  $fields
14
     * @return \Phalcon\Mvc\Model
15
     */
16 1
    public function getAllCustomFields(array $fields = [], int $company_id = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $company_id is not used and could be removed. ( Ignorable by Annotation )

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

16
    public function getAllCustomFields(array $fields = [], /** @scrutinizer ignore-unused */ int $company_id = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18
        //We does it only find names in plural? We need to fix this or make a workaroun
19 1
        if (!$models = Modules::findFirstByName($this->getSource())) {
0 ignored issues
show
Bug introduced by
The method findFirstByName() does not exist on Gewaer\Models\Modules. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

19
        if (!$models = Modules::/** @scrutinizer ignore-call */ findFirstByName($this->getSource())) {
Loading history...
20
            return;
21
        }
22
23 1
        $conditions = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $conditions is dead and can be removed.
Loading history...
24 1
        $fieldsIn = null;
25
26 1
        if (!empty($fields)) {
27
            $fieldsIn = " and name in ('" . implode("','", $fields) . ')';
28
        }
29
30 1
        $conditions = 'modules_id = ? ' . $fieldsIn;
31
32 1
        $bind = [$this->getId(),$this->di->getApp()->getId(), $models->getId(), $this->di->getUserData()->default_company];
0 ignored issues
show
Bug introduced by
The method getApp() does not exist on Phalcon\Mvc\Model\Resultset. ( Ignorable by Annotation )

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

32
        $bind = [$this->getId(),$this->di->/** @scrutinizer ignore-call */ getApp()->getId(), $models->getId(), $this->di->getUserData()->default_company];

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...
Bug introduced by
The method getUserData() does not exist on Phalcon\Mvc\Model\Resultset. ( Ignorable by Annotation )

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

32
        $bind = [$this->getId(),$this->di->getApp()->getId(), $models->getId(), $this->di->/** @scrutinizer ignore-call */ getUserData()->default_company];

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...
Bug Best Practice introduced by
The property di does not exist on Gewaer\CustomFields\AbstractCustomFieldsModel. Since you implemented __get, consider adding a @property annotation.
Loading history...
33
34
35
        // $customFieldsValueTable = $this->getSource() . '_custom_fields';
36 1
        $customFieldsValueTable = $this->getSource() . '_custom_fields';
37
38
        //We are to make a new query to replace old gewaer implementation.
39 1
        $result = $this->getReadConnection()->prepare("SELECT l.{$this->getSource()}_id,
1 ignored issue
show
Bug introduced by
The method prepare() does not exist on Phalcon\Db\AdapterInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Phalcon\Db\Adapter. Are you sure you never get one of those? ( Ignorable by Annotation )

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

39
        $result = $this->getReadConnection()->/** @scrutinizer ignore-call */ prepare("SELECT l.{$this->getSource()}_id,
Loading history...
40
                                               c.id as field_id,
41
                                               c.name,
42
                                               l.value ,
43
                                               c.users_id,
44
                                               l.created_at,
45
                                               l.updated_at
46 1
                                        FROM {$customFieldsValueTable} l,
47
                                             custom_fields c
48
                                        WHERE c.id = l.custom_fields_id
49 1
                                          AND l.{$this->getSource()}_id = ?
50
                                          AND c.apps_id = ?
51
                                          AND c.modules_id = ?
52
                                          AND c.companies_id = ? 
53
                                          AND l.companies_id = c.companies_id");
54
55 1
        $result->execute($bind);
56
57
        // $listOfCustomFields = $result->fetchAll();
58
        $listOfCustomFields = [];
59
60
        while ($row = $result->fetch(\PDO::FETCH_OBJ)) {
61
            $listOfCustomFields[$row->name] = $row->value;
62
        }
63
64
        return $listOfCustomFields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $listOfCustomFields returns the type array which is incompatible with the documented return type Phalcon\Mvc\Model.
Loading history...
65
    }
66
67
    /**
68
     * Get all custom fields of the given model
69
     *
70
     * @param  array  $fields
71
     * @return \Phalcon\Mvc\Model
72
     */
73
    public function getCustomFieldsByModel($modelName)
74
    {
75
        if (!$module = Modules::findFirstByName($modelName)) {
76
            return;
77
        }
78
        $allFields = [];
79
        if ($fields = \Incursio\Models\CustomFields::findByModulesId($module->id)->toArray()) {
0 ignored issues
show
Bug introduced by
The type Incursio\Models\CustomFields was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
            foreach ($fields as $field) {
81
                array_push($allFields, $field['name']);
82
            }
83
            return $allFields;
84
        }
85
    }
86
}
87