Intraface_Controller_Signup::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 2
1
<?php
2
/**
3
 * Signup
4
 *
5
 * @todo Could easily be changed so one could create oneself on an intranet, you have a code for
6
 *
7
 * @package Intraface
8
 * @author  Lars Olesen <[email protected]>
9
 * @since   0.1.0
10
 * @version @package-version@
11
 */
12
class Intraface_Controller_Signup extends k_Component
13
{
14
    public $msg = '';
15
    public $errors = array();
16
    protected $kernel;
17
    protected $template;
18
    protected $mdb2;
19
20
    function __construct(k_TemplateFactory $template, MDB2_Driver_Common $mdb2)
21
    {
22
        $this->mdb2 = $mdb2;
23
        $this->template = $template;
24
    }
25
26
    function execute()
27
    {
28
        $this->url_state->init("continue", $this->url('/login'));
29
        return parent::execute();
30
    }
31
32 View Code Duplication
    function renderHtml()
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...
33
    {
34
        $this->document->setTitle('Signup');
35
36
        $smarty = $this->template->create(dirname(__FILE__) . '/templates/signup');
37
        return $smarty->render($this);
38
    }
39
40
    function postForm()
41
    {
42
        if (!Validate::email($this->body('email'))) {
43
            $this->error[] = 'E-mail ugyldig';
0 ignored issues
show
Bug introduced by
The property error does not seem to exist. Did you mean errors?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
44
        }
45
        if (!Validate::string($this->body('password'), VALIDATE_ALPHA . VALIDATE_NUM)) {
46
            $this->error[] = 'Password ugyldigt';
0 ignored issues
show
Bug introduced by
The property error does not seem to exist. Did you mean errors?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
47
        }
48
        if (!empty($error) and count($error) > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
49
            $this->msg = 'Vi kunne ikke oprette dig';
50
            return $this->render();
51
        } else {
52
            $db = $this->mdb2;
53
            $res = $db->query("SELECT id FROM user WHERE email = ".$db->quote($this->body('email'), 'text'));
54
            if (PEAR::isError($res)) {
55
                throw new Exception($res->getMessage());
56
            }
57
            if ($res->numRows() == 0) {
58
                $res = $db->query("INSERT INTO user SET email = ".$db->quote($this->body('email'), 'text').", password=".$db->quote(md5($this->body('password')), 'text'));
0 ignored issues
show
Unused Code introduced by
$res 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...
59
                $user_id = $db->lastInsertID('user');
60
            } else {
61
                $this->error[] = 'Du er allerede oprettet';
0 ignored issues
show
Bug introduced by
The property error does not seem to exist. Did you mean errors?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
            }
63
64
            if (!empty($error) and count($error) > 0) {
0 ignored issues
show
Bug introduced by
The variable $error seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
65
                $this->msg = 'Du er allerede oprettet. <a href="'.url('/login').'">Login</a>.';
66
                return $this->render();
67
            } else {
68
                require_once 'Intraface/modules/intranetmaintenance/IntranetMaintenance.php';
69
                $intranet = new IntranetMaintenance();
70
                $data = array('identifier' => $this->body('identifier'), 'name' => $this->body('name'));
71
                if (!$intranet->save($data)) {
72
                    $this->msg = $intranet->error->view();
0 ignored issues
show
Bug introduced by
The property error does not seem to exist in IntranetMaintenance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
                } else {
74
                    $intranet_id = $intranet->getId(); // betatest intranet for forskellige brugere
75
76
                    // intranet access
77
                    $db->query("INSERT INTO permission SET intranet_id = ".$db->quote($intranet_id, 'integer').", user_id = ".$db->quote($user_id, 'integer'));
0 ignored issues
show
Bug introduced by
The variable $user_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...
78
79
                    // module access
80
                    $modules = array('administration', 'modulepackage', 'onlinepayment', 'cms', 'filemanager', 'contact', 'debtor','quotation', 'invoice', 'order','accounting', 'product', 'stock', 'webshop');
81
82
                    foreach ($modules as $module) {
83
                        $res = $db->query("SELECT id FROM module WHERE name = ".$db->quote($module, 'text')." LIMIT 1");
84
                        if ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
85
                            $db->query("INSERT INTO permission SET
86
                                intranet_id = ".$db->quote($intranet_id, 'integer').",
87
                                user_id = ".$db->quote($user_id, 'integer').",
88
                                module_id = ".$db->quote($row['id'], 'integer'));
89
                            $db->query("INSERT INTO permission SET
90
                                intranet_id = ".$db->quote($intranet_id, 'integer').",
91
                                user_id = ".$db->quote(0, 'integer').",
92
                                module_id = ".$db->quote($row['id'], 'integer'));
93
                        }
94
                    }
95
96
                    $sub_access = array('edit_templates', 'setting', 'vat_report', 'endyear');
97
98
                    foreach ($sub_access as $module) {
99
                        $res = $db->query("SELECT id, module_id FROM module_sub_access WHERE name = ".$db->quote($module, 'text')." LIMIT 1");
100
                        if ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) {
101
                            $res = $db->query("INSERT INTO permission SET intranet_id = ".$db->quote($intranet_id, 'integer').", module_sub_access_id = ".$db->quote($row['id'], 'integer').", module_id = ".$db->quote($row['module_id'], 'integer').", user_id = ".$db->quote($user_id, 'integer'));
102
                            if (PEAR::isError($res)) {
103
                                throw new Exception($res->getUserInfo());
104
                                $this->error[] = 'Kunne ikke oprette nogle af rettighederne';
0 ignored issues
show
Unused Code introduced by
$this->error[] = 'Kunne ...ogle af rettighederne'; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
105
                            }
106
                        }
107
                    }
108
                    $user = new Intraface_User($user_id);
109
                    $user->setActiveIntranetId($intranet_id);
110
111
                    return new k_SeeOther($this->url('../login'));
112
                }
113
            }
114
        }
115
        return $this->render();
116
    }
117
}
118