Completed
Push — master ( 00e474...9d3fbd )
by Michael
04:26
created

OledrionOledrion_vendorsHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 12.05 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 10
loc 83
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getAllVendors() 0 19 1
A getVendorProductsCount() 0 6 1
A deleteVendor() 0 4 1
A getVendorsFromIds() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 28 and the first side effect is on line 23.

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * oledrion
14
 *
15
 * @copyright   {@link http://xoops.org/ XOOPS Project}
16
 * @license     {@link http://www.fsf.org/copyleft/gpl.html GNU public license}
17
 * @author      Hervé Thouzard (http://www.herve-thouzard.com/)
18
 */
19
20
/**
21
 * Gestion des vendeurs
22
 */
23
require __DIR__ . '/classheader.php';
24
25
/**
26
 * Class Oledrion_vendors
27
 */
28
class Oledrion_vendors extends Oledrion_Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
29
{
30
    /**
31
     * constructor
32
     *
33
     * normally, this is called from child classes only
34
     *
35
     * @access public
36
     */
37
    public function __construct()
38
    {
39
        $this->initVar('vendor_id', XOBJ_DTYPE_INT, null, false);
40
        $this->initVar('vendor_name', XOBJ_DTYPE_TXTBOX, null, false);
41
    }
42
}
43
44
/**
45
 * Class OledrionOledrion_vendorsHandler
46
 */
47
class OledrionOledrion_vendorsHandler extends Oledrion_XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
48
{
49
    /**
50
     * OledrionOledrion_vendorsHandler constructor.
51
     * @param XoopsDatabase|null $db
52
     */
53
    public function __construct(XoopsDatabase $db)
54
    { //                            Table               Classe          Id          Libellé
55
        parent::__construct($db, 'oledrion_vendors', 'oledrion_vendors', 'vendor_id', 'vendor_name');
56
    }
57
58
    /**
59
     * Renvoie la liste de tous les vendeurs du module
60
     *
61
     * @param  Oledrion_parameters $parameters
62
     * @return array               tableau d'objets de type vendors
63
     * @internal param int $start Position de départ
64
     * @internal param int $limit Nombre total d'enregistrements à renvoyer
65
     * @internal param string $order Champ sur lequel faire le tri
66
     * @internal param string $order Ordre du tri
67
     * @internal param bool $idaskey Indique si le tableau renvoyé doit avoir pour clé l'identifiant unique de l'enregistrement
68
     */
69
    public function getAllVendors(Oledrion_parameters $parameters)
70
    {
71
        $parameters = $parameters->extend(new Oledrion_parameters(array(
0 ignored issues
show
Documentation introduced by
new \Oledrion_parameters...C', 'idaskey' => true)) is of type object<Oledrion_parameters>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
                                                                      'start'   => 0,
73
                                                                      'limit'   => 0,
74
                                                                      'sort'    => 'vendor_name',
75
                                                                      'order'   => 'ASC',
76
                                                                      'idaskey' => true
77
                                                                  )));
78
        $critere    = new Criteria('vendor_id', 0, '<>');
79
        $critere->setLimit($parameters['limit']);
80
        $critere->setStart($parameters['start']);
81
        $critere->setSort($parameters['sort']);
82
        $critere->setOrder($parameters['order']);
83
        $categories = array();
0 ignored issues
show
Unused Code introduced by
$categories is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
        $categories = $this->getObjects($critere, $parameters['idaskey']);
85
86
        return $categories;
87
    }
88
89
    /**
90
     * Retourne le nombre de produits associés à un vendeur
91
     *
92
     * @param  integer $vendor_id L'ID du vendeur
93
     * @return integer Le nombre de produits du vendeur
94
     */
95
    public function getVendorProductsCount($vendor_id)
96
    {
97
        global $h_oledrion_products;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
98
99
        return $h_oledrion_products->getVendorProductsCount($vendor_id);
100
    }
101
102
    /**
103
     * Supprime un vendeur
104
     *
105
     * @param  oledrion_vendors $vendor
106
     * @return boolean          Le résultat de la suppression
107
     */
108
    public function deleteVendor(Oledrion_vendors $vendor)
109
    {
110
        return $this->delete($vendor, true);
111
    }
112
113
    /**
114
     * Retourne des vendeurs selon leur ID
115
     *
116
     * @param  array $ids Les ID des vendeurs à retrouver
117
     * @return array Objets de type oledrion_vendors
118
     */
119 View Code Duplication
    public function getVendorsFromIds($ids)
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...
120
    {
121
        $ret = array();
122
        if (is_array($ids) && count($ids) > 0) {
123
            $criteria = new Criteria('vendor_id', '(' . implode(',', $ids) . ')', 'IN');
124
            $ret      = $this->getObjects($criteria, true, true, '*', false);
125
        }
126
127
        return $ret;
128
    }
129
}
130