Passed
Push — master ( 6494fd...7e1229 )
by Nicklas
02:09
created

CreateQuestionForm::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 0
cts 20
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nicklas\Comment\HTMLForm\Comment;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Nicklas\Comment\Modules\User;
8
use \Nicklas\Comment\Modules\Comment;
9
10
/**
11
 * Example of FormModel implementation.
12
 */
13
class CreateQuestionForm extends FormModel
14
{
15
    /**
16
     * Constructor injects with DI container.
17
     *
18
     * @param Anax\DI\DIInterface $di a service container
19
     */
20
    public function __construct(DIInterface $di)
21
    {
22
        parent::__construct($di);
23
        $this->form->create(
24
            [
25
                "id" => __CLASS__,
26
            ],
27
            [
28
                "title" => [
29
                    "type"        => "text",
30
                    "label" => "En bra titel ökar chansen att någon svarar",
31
                    "placeholder" => "Titel"
32
                ],
33
                "tags" => [
34
                    "type"        => "text",
35
                    "label" => "Du kan ange fler än en tag. Men du behöver ange minst en.",
36
                    "placeholder" => "#tags"
37
                ],
38
                "question" => [
39
                    "type"        => "textarea",
40
                    "label" => "Här kan du skriva din fråga",
41
                    "placeholder" => "Din fråga"
42
                ],
43
44
                "submit" => [
45
                    "type" => "submit",
46
                    "value" => "Skicka",
47
                    "callback" => [$this, "callbackSubmit"]
48
                ],
49
            ]
50
        );
51
    }
52
53
54
55
    public function getHashtags($string)
56
    {
57
        preg_match_all("/(#\w+)/u", $string, $matches);
58
        if ($matches) {
59
            $hashtagsArray = array_count_values($matches[0]);
60
            $hashtags = array_keys($hashtagsArray);
61
            $tags = implode(",", $hashtags);
62
            return $tags;
63
        }
64
        $tags = implode(",", $hashtags);
0 ignored issues
show
Bug introduced by
The variable $hashtags seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Unused Code introduced by
$tags 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...
65
        return null;
66
    }
67
    /**
68
     * Callback for submit-button which should return true if it could
69
     * carry out its work and false if something failed.
70
     *
71
     * @return boolean true if okey, false if something went wrong.
72
     */
73
    public function callbackSubmit()
74
    {
75
        // Get values from the submitted form
76
        $title          = $this->form->value("title");
77
        $tags           = $this->getHashtags($this->form->value("tags"));
78
        $question       = $this->form->value("question");
79
80
        $comment = new Comment();
81
        $comment->setDb($this->di->get("db"));
82
83
        if (!$this->di->get('session')->has("user")) {
84
            $this->form->addOutput("Du behöver logga in");
85
            return false;
86
        }
87
88
        if (!$tags) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tags of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
89
            $this->form->addOutput("
90
            <p>Inga taggar hittades, du behöver ange minst en.</p>
91
            <p>Du taggar genom att göra en hashtag, #mintag.</p>
92
            <p>#hej på dig #kaffe <--- kommer att bli #hej #kaffe</p>
93
            ");
94
            return false;
95
        }
96
97
        $user = $this->di->get('session')->get("user"); # get user name
98
99
        $comment->user       = $user;
0 ignored issues
show
Bug introduced by
The property user does not seem to exist in Nicklas\Comment\Modules\Comment.

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...
100
        $comment->title      = $title;
101
        $comment->tags       = $tags;
102
        $comment->text       = $question;
103
        $comment->type       = "question";
104
        $comment->save();
105
106
        $this->form->addOutput("Du skapade en fråga! Du bör se den nedan");
107
        return true;
108
    }
109
}
110