Userhome::preparePage()   D
last analyzed

Complexity

Conditions 14
Paths 161

Size

Total Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 4.8684
c 0
b 0
f 0
cc 14
nc 161
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
    HCSF - A multilingual CMS and Shopsystem
5
    Copyright (C) 2014  Marcus Haase - [email protected]
6
7
    This program is free software: you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation, either version 3 of the License, or
10
    (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
17
    You should have received a copy of the GNU General Public License
18
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
namespace HaaseIT\HCSF\Controller\Customer;
22
23
use HaaseIT\Toolbox\Tools;
24
use Zend\ServiceManager\ServiceManager;
25
26
/**
27
 * Class Userhome
28
 * @package HaaseIT\HCSF\Controller\Customer
29
 */
30
class Userhome extends Base
31
{
32
    /**
33
     * @var \HaaseIT\Toolbox\Textcat
34
     */
35
    private $textcats;
36
37
    /**
38
     * @var \PDO
39
     */
40
    private $db;
41
42
    /**
43
     * Userhome constructor.
44
     * @param ServiceManager $serviceManager
45
     */
46
    public function __construct(ServiceManager $serviceManager)
47
    {
48
        parent::__construct($serviceManager);
49
        $this->textcats = $serviceManager->get('textcats');
50
        $this->db = $serviceManager->get('db');
51
    }
52
53
    /**
54
     *
55
     */
56
    public function preparePage()
57
    {
58
        $this->P = new \HaaseIT\HCSF\CorePage($this->serviceManager);
59
        $this->P->cb_pagetype = 'content';
60
61
        if (!$this->helperCustomer->getUserData()) {
62
            $this->P->oPayload->cl_html = $this->textcats->T('denied_notloggedin');
63
        } else {
64
            $this->P->cb_customcontenttemplate = 'customer/customerhome';
65
66
            $aPData['display_logingreeting'] = false;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aPData was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aPData = 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...
67
            if (filter_input(INPUT_GET, 'login') !== null) {
68
                $aPData['display_logingreeting'] = true;
69
            }
70
            if (filter_input(INPUT_GET, 'editprofile') !== null) {
71
                $aErr = [];
72
73
                if (filter_input(INPUT_POST, 'doEdit') === 'yes') {
74
                    $sql = 'SELECT '.DB_ADDRESSFIELDS.' FROM customer WHERE cust_id != :id AND cust_email = :email';
75
76
                    $sEmail = filter_var(trim(Tools::getFormfield('email')), FILTER_SANITIZE_EMAIL);
77
78
                    $hResult = $this->db->prepare($sql);
79
                    $hResult->bindValue(':id', $_SESSION['user']['cust_id'], \PDO::PARAM_INT);
80
                    $hResult->bindValue(':email', $sEmail, \PDO::PARAM_STR);
81
                    $hResult->execute();
82
                    $iRows = $hResult->rowCount();
83
                    if ($iRows == 1) {
84
                        $aErr['adrform_error_emailalreadytaken'] = true;
85
                    }
86
                    $aErr = $this->helperCustomer->validateCustomerForm($this->config->getLang(), $aErr, true);
87
88
                    if (empty($aErr)) {
89
                        if ($this->config->getCustomer('allow_edituserprofile')) {
90
                            $aData = [
91
                                //'cust_email' => $sEmail, // disabled until renwewd email verification implemented
92
                                'cust_corp' => filter_var(trim(Tools::getFormfield('corpname')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
93
                                'cust_name' => filter_var(trim(Tools::getFormfield('name')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
94
                                'cust_street' => filter_var(trim(Tools::getFormfield('street')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
95
                                'cust_zip' => filter_var(trim(Tools::getFormfield('zip')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
96
                                'cust_town' => filter_var(trim(Tools::getFormfield('town')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
97
                                'cust_phone' => filter_var(trim(Tools::getFormfield('phone')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
98
                                'cust_cellphone' => filter_var(trim(Tools::getFormfield('cellphone')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
99
                                'cust_fax' => filter_var(trim(Tools::getFormfield('fax')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
100
                                'cust_country' => filter_var(trim(Tools::getFormfield('country')), FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW),
101
                            ];
102
                        }
103
                        $postpwd = filter_input(INPUT_POST, 'pwd');
104
                        if (!empty($postpwd)) {
105
                            $aData['cust_password'] = password_hash($postpwd, PASSWORD_DEFAULT);
0 ignored issues
show
Bug introduced by
The variable $aData 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...
106
                            $aPData['infopasswordchanged'] = true;
107
                        }
108
                        $aData['cust_id'] = $_SESSION['user']['cust_id'];
109
110
                        if (count($aData) > 1) {
111
                            $sql = \HaaseIT\Toolbox\DBTools::buildPSUpdateQuery($aData, 'customer', 'cust_id');
112
                            $hResult = $this->db->prepare($sql);
113
                            foreach ($aData as $sKey => $sValue) {
114
                                $hResult->bindValue(':'.$sKey, $sValue);
115
                            }
116
                            $hResult->execute();
117
                            $aPData['infochangessaved'] = true;
118
                        } else {
119
                            $aPData['infonothingchanged'] = true;
120
                        }
121
                    }
122
                }
123
                $this->P->cb_customdata['customerform'] = $this->helperCustomer->buildCustomerForm(
124
                    $this->config->getLang(),
125
                    'editprofile',
126
                    $aErr
127
                );
128
                //if (HelperConfig::$customer["allow_edituserprofile"]) $P["lang"]["cl_html"] .= '<br>'.$this->textcats->T("userprofile_infoeditemail"); // Future implementation
129
            } else {
130
                $this->P->cb_customdata['customerform'] = $this->helperCustomer->buildCustomerForm(
131
                    $this->config->getLang(),
132
                    'userhome'
133
                );
134
            }
135
            $aPData['showprofilelinks'] = false;
136
            if (filter_input(INPUT_GET, 'editprofile') === null) {
137
                $aPData['showprofilelinks'] = true;
138
            }
139
            if (isset($aPData) && count($aPData)) {
140
                $this->P->cb_customdata['userhome'] = $aPData;
141
            }
142
        }
143
    }
144
}
145