Passed
Push — master ( 005d4f...8b5e26 )
by Michael
06:49
created

Animal::getNumOfFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 27
rs 9.9332
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 36 and the first side effect is on line 31.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace XoopsModules\Pedigree;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
/**
15
 * Pedigree module for XOOPS
16
 *
17
 * @package     XoopsModules\Pedigree
18
 * @copyright   {@link https://github.com/Xoops The XOOPS Project}
19
 * @license     {@link https://www.fsf.org/copyleft/gpl.html GNU public license}
20
 * @since       1.3.1
21
 * @author      XOOPS Module Dev Team
22
 * @author      ZySpec <[email protected]>
23
 */
24
use XoopsModules\Pedigree\{
25
    Constants,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Pedigree\Constants. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
26
    Field,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Pedigree\Field. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
27
    FieldsHandler,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Pedigree\FieldsHandler. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
    TreeHandler
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Pedigree\TreeHandler. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
29
};
30
31
defined('XOOPS_ROOT_PATH') || die('Restricted access');
32
33
/**
34
 * Animal Class
35
 */
36
class Animal
37
{
38
    protected $myTree = [];
39
    protected $fields = [];       // array of ids for fields
40
    protected $configValues = []; // id indexed array of field info
41
    private   $helper;            // module Helper object
42
    private   $treeHandler;       // Pedigree Tree handler object
43
44
    /**
45
     * Animal class constructor
46
     *
47
     * initializes the tree array
48
     * @param int|null $id
49
     */
50
    public function __construct(?int $id = null)
51
    {
52
        $id = !empty($id) ? (int) $id : Constants::DEFAULT_TREE_ID;
53
54
        /**
55
         * @var Helper $this->helper
56
         * @var TreeHandler $this->treeHandler;
57
         */
58
        $this->helper      = Helper::getInstance();
59
        $this->treeHandler = $this->helper->getHandler('Tree');
60
        $this->myTree      = $this->treeHandler->get($id);
61
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
62
        $SQL = "SELECT * FROM " . $GLOBALS['xoopsDB']->prefix("pedigree_tree") . " WHERE id = {$id}";
63
        $result    = $GLOBALS['xoopsDB']->query($SQL);
64
        $row       = $GLOBALS['xoopsDB']->fetchRow($result);
65
        $numfields = mysqli_num_fields($result);
66
        for ($i = 0; $i < $numfields; ++$i) {
67
            $key        =$GLOBALS['xoopsDB']->getFieldName($result, $i);
68
            $this->$key = $row[$i];
69
        }
70
        */
71
    }
72
73
    /**
74
     * Get Ids of Fields
75
     *
76
     * @return array
77
     */
78
    public function getFieldsIds(): array
79
    {
80
        /** @var FieldsHandler $fieldsHandler */
81
        $fieldsHandler = $this->helper->getHandler('Fields');
82
        $criteria = new \Criteria('');
83
        $criteria->setSort('order');
84
        $criteria->order = 'ASC';
85
        //$this->fields = $fieldsHandler->getIds($criteria); //get all object IDs
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
86
        $this->configValues = $fieldsHandler->getAll($criteria, null, false, true); //get array of fields w/ id as array key
87
        $this->fields = array_keys($this->configValues);
88
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
89
        $SQL    = "SELECT * FROM " . $GLOBALS['xoopsDB']->prefix("pedigree_fields") . " ORDER BY `order`";
90
        $result = $GLOBALS['xoopsDB']->query($SQL);
91
        $fields = array();
92
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
93
            $fields[] = $row['id'];
94
            $configValues[] = $row;
95
96
        }
97
        $this->configValues = isset($configValues) ? $configValues : '';
98
        //print_r ($this->configValues); die();
99
        */
100
        unset($fieldsHandler, $criteria);
101
102
        return $this->fields;
103
    }
104
105
    /**
106
     * Get indexed array of field configs
107
     *
108
     * @return array
109
     */
110
    public function getConfig(): array
111
    {
112
        return $this->configValues;
113
    }
114
115
    /**
116
     * Get Fields Elements to display in a form
117
     *
118
     * example usage: $formElements = $animal->getFormFieldsElements();
119
     *                foreach ($formElements as $fElement) {
120
     *                   $form->addElement($fElement);
121
     *                }
122
     *
123
     * @return array form element array to be added to form
124
     */
125
    public function getFormFieldsElements(): array
126
    {
127
        $formElements = [];
128
        $this->fields = $this->getFieldsIds();
129
        $fieldCount = count($this->fields);
130
        for ($i = 0; $i < $fieldCount; ++$i) {
131
            $userField = new Field($this->fields[$i], $this->getConfig());
132
            if ($userField->isActive()) {
133
                $fieldType = $userField->getSetting('fieldtype');
134
                $fieldObject = new $fieldType($userField, $this);
135
                $edditable[$i] = $fieldObject->editField();
136
                $formElements[] = $edditable[$i];
137
            }
138
        }
139
        return $formElements;
140
    }
141
}
142