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

getThisLocationThisDelivery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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 25 and the first side effect is on line 20.

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      Hossein Azizabadi ([email protected])
18
 */
19
20
require __DIR__ . '/classheader.php';
21
22
/**
23
 * Class Oledrion_delivery
24
 */
25
class Oledrion_delivery 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...
26
{
27
    /**
28
     * constructor
29
     *
30
     * normally, this is called from child classes only
31
     *
32
     * @access public
33
     */
34
    public function __construct()
35
    {
36
        $this->initVar('delivery_id', XOBJ_DTYPE_INT, null, false);
37
        $this->initVar('delivery_title', XOBJ_DTYPE_TXTBOX, null, false);
38
        $this->initVar('delivery_description', XOBJ_DTYPE_TXTAREA, null, false);
39
        $this->initVar('delivery_online', XOBJ_DTYPE_INT, null, false);
40
        $this->initVar('delivery_image', XOBJ_DTYPE_TXTBOX, null, false);
41
42
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 1, false);
43
    }
44
45
    /**
46
     * Retourne l'URL de l'image de la catégorie courante
47
     * @return string L'URL
48
     */
49 View Code Duplication
    public function getPictureUrl()
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...
50
    {
51
        if (xoops_trim($this->getVar('product_image_url')) != '') {
52
            return OLEDRION_PICTURES_URL . '/' . $this->getVar('delivery_image');
53
        } else {
54
            return '';
55
        }
56
    }
57
58
    /**
59
     * Indique si l'image de la catégorie existe
60
     *
61
     * @return boolean Vrai si l'image existe sinon faux
62
     */
63 View Code Duplication
    public function pictureExists()
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...
64
    {
65
        $return = false;
66
        if (xoops_trim($this->getVar('delivery_image')) != ''
67
            && file_exists(OLEDRION_PICTURES_PATH . '/' . $this->getVar('delivery_image'))
68
        ) {
69
            $return = true;
70
        }
71
72
        return $return;
73
    }
74
75
    /**
76
     * Supprime l'image associée à une catégorie
77
     * @return void
78
     */
79 View Code Duplication
    public function deletePicture()
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...
80
    {
81
        if ($this->pictureExists()) {
82
            @unlink(OLEDRION_PICTURES_PATH . '/' . $this->getVar('delivery_image'));
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...
83
        }
84
        $this->setVar('delivery_image', '');
85
    }
86
87
    /**
88
     * Retourne les éléments du produits formatés pour affichage
89
     *
90
     * @param  string $format
91
     * @return array
92
     */
93 View Code Duplication
    public function toArray($format = 's')
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...
94
    {
95
        global $h_oledrion_location_delivery;
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...
96
        $ret                       = array();
0 ignored issues
show
Unused Code introduced by
$ret 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...
97
        $ret                       = parent::toArray($format);
98
        $ret['delivery_image_url'] = $this->getPictureUrl();
99
100
        return $ret;
101
    }
102
}
103
104
/**
105
 * Class OledrionOledrion_deliveryHandler
106
 */
107
class OledrionOledrion_deliveryHandler 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...
108
{
109
    /**
110
     * OledrionOledrion_deliveryHandler constructor.
111
     * @param XoopsDatabase|null $db
112
     */
113
    public function __construct(XoopsDatabase $db)
114
    { //                                        Table                   Classe              Id
115
        parent::__construct($db, 'oledrion_delivery', 'oledrion_delivery', 'delivery_id');
116
    }
117
118
    /**
119
     * @param  Oledrion_parameters $parameters
120
     * @return array
121
     */
122 View Code Duplication
    public function getAllDelivery(Oledrion_parameters $parameters)
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...
123
    {
124
        $parameters = $parameters->extend(new Oledrion_parameters(array(
0 ignored issues
show
Documentation introduced by
new \Oledrion_parameters...id', 'order' => 'ASC')) 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...
125
                                                                      'start' => 0,
126
                                                                      'limit' => 0,
127
                                                                      'sort'  => 'delivery_id',
128
                                                                      'order' => 'ASC'
129
                                                                  )));
130
        $critere    = new Criteria('delivery_id', 0, '<>');
131
        $critere->setLimit($parameters['limit']);
132
        $critere->setStart($parameters['start']);
133
        $critere->setSort($parameters['sort']);
134
        $critere->setOrder($parameters['order']);
135
        $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...
136
        $categories = $this->getObjects($critere);
137
138
        return $categories;
139
    }
140
141
    /**
142
     * @param  Oledrion_parameters $parameters
143
     * @return array
144
     */
145
    public function getLocationDelivery(Oledrion_parameters $parameters)
146
    {
147
        global $h_oledrion_location_delivery;
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...
148
        $ret               = array();
149
        $parameters        = $parameters->extend(new Oledrion_parameters(array(
0 ignored issues
show
Documentation introduced by
new \Oledrion_parameters...SC', 'location' => '')) 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...
150
                                                                             'start'    => 0,
151
                                                                             'limit'    => 0,
152
                                                                             'sort'     => 'delivery_id',
153
                                                                             'order'    => 'ASC',
154
                                                                             'location' => ''
155
                                                                         )));
156
        $location_delivery = $h_oledrion_location_delivery->getLocationDeliveryId($parameters);
157
158
        $critere = new CriteriaCompo();
159
        $critere->setLimit($parameters['limit']);
160
        $critere->setStart($parameters['start']);
161
        $critere->setSort($parameters['sort']);
162
        $critere->setOrder($parameters['order']);
163
        $obj = $this->getObjects($critere);
164
        if ($obj) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $obj of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
165
            foreach ($obj as $root) {
166
                $tab = array();
0 ignored issues
show
Unused Code introduced by
$tab 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...
167
                $tab = $root->toArray();
168
                if (isset($location_delivery[$root->getVar('delivery_id')]['ld_delivery'])
169
                    && $location_delivery[$root->getVar('delivery_id')]['ld_delivery'] == $root->getVar('delivery_id')
170
                ) {
171
                    $tab['ld_id']['delivery_select']  = 1;
172
                    $tab['ld_id']['ld_id']            = $location_delivery[$root->getVar('delivery_id')]['ld_id'];
173
                    $tab['ld_id']['ld_location']      = $location_delivery[$root->getVar('delivery_id')]['ld_location'];
174
                    $tab['ld_id']['ld_delivery']      = $location_delivery[$root->getVar('delivery_id')]['ld_delivery'];
175
                    $tab['ld_id']['ld_price']         = $location_delivery[$root->getVar('delivery_id')]['ld_price'];
176
                    $tab['ld_id']['ld_delivery_time'] = $location_delivery[$root->getVar('delivery_id')]['ld_delivery_time'];
177
                }
178
                $ret[] = $tab;
179
            }
180
        }
181
182
        return $ret;
183
    }
184
185
    /**
186
     * @param $location_id
187
     * @return array
188
     */
189
    public function getThisLocationDelivery($location_id)
190
    {
191
        global $h_oledrion_location_delivery;
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...
192
        $oledrion_Currency = Oledrion_Currency::getInstance();
193
        $ret               = array();
194
        $parameters        = array('location' => $location_id);
195
        $location_delivery = $h_oledrion_location_delivery->getLocationDeliveryId($parameters);
196
        foreach ($location_delivery as $location) {
197
            $id[] = $location['ld_delivery'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$id was never initialized. Although not strictly required by PHP, it is generally a good practice to add $id = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
198
        }
199
200
        $critere = new CriteriaCompo();
201
        $critere->add(new Criteria('delivery_id', '(' . implode(',', $id) . ')', 'IN'));
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
202
        $critere->add(new Criteria('delivery_online', 1));
203
        $obj = $this->getObjects($critere);
204
        if ($obj) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $obj of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
205
            foreach ($obj as $root) {
206
                $tab                              = array();
0 ignored issues
show
Unused Code introduced by
$tab 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...
207
                $tab                              = $root->toArray();
208
                $tab['delivery_price']            = $location_delivery[$root->getVar('delivery_id')]['ld_price'];
209
                $tab['delivery_price_fordisplay'] = $oledrion_Currency->amountForDisplay($tab['delivery_price']);
210
                $tab['delivery_time']             = $location_delivery[$root->getVar('delivery_id')]['ld_delivery_time'];
211
                $ret[]                            = $tab;
212
            }
213
        }
214
215
        return $ret;
216
    }
217
218
    /**
219
     * @param $location_id
220
     * @param $delivery_id
221
     * @return array
222
     */
223
    public function getThisLocationThisDelivery($location_id, $delivery_id)
224
    {
225
        global $h_oledrion_location_delivery;
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...
226
        $location_delivery     = $h_oledrion_location_delivery->getDelivery($location_id, $delivery_id);
227
        $ret                   = array();
0 ignored issues
show
Unused Code introduced by
$ret 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...
228
        $obj                   = $this->get($location_id);
229
        $ret                   = $obj->toArray();
230
        $ret['delivery_price'] = $location_delivery['ld_price'];
231
        $ret['delivery_time']  = $location_delivery['ld_delivery_time'];
232
233
        return $ret;
234
    }
235
}
236