Completed
Pull Request — master (#20)
by Fredrik
03:15
created

QuestionsController   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 453
Duplicated Lines 3.31 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 9
Bugs 2 Features 2
Metric Value
wmc 36
c 9
b 2
f 2
lcom 1
cbo 8
dl 15
loc 453
rs 8.8

14 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 15 1
A setupAction() 0 5 1
A recentquestionsAction() 0 17 2
C listAction() 0 92 10
B acceptAnswer() 0 13 5
A showformAction() 0 5 1
A showFormCommentOrAnswer() 0 22 3
B singleAction() 0 104 4
A askAction() 15 15 2
B createAddQuestionForm() 0 34 2
B callbackSubmitAddQuestion() 0 31 2
A callbackSubmitFailAddQuestion() 0 6 1
A callbackSuccess() 0 6 1
A callbackFail() 0 6 1

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
2
3
namespace Anax\Questions;
4
5
/**
6
 * A controller for "Questions".
7
 *
8
 */
9
class QuestionsController implements \Anax\DI\IInjectionAware
10
{
11
    use \Anax\DI\TInjectable,
12
    \Anax\MVC\TRedirectHelpers;
13
14
    /**
15
     * Initialize the controller.
16
     *
17
     * @return void
18
     */
19
    public function initialize()
20
    {
21
        $this->questions = new \Anax\Questions\CQuestions();
0 ignored issues
show
Bug introduced by
The property questions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
        $this->questions->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
23
        $this->tags = new \Anax\Tags\CTags();
0 ignored issues
show
Bug introduced by
The property tags does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->tags->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
25
        $this->tags2Questions = new \Anax\Tags\CTags2Question();
0 ignored issues
show
Bug introduced by
The property tags2Questions does not seem to exist. Did you mean questions?

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...
26
        $this->tags2Questions->setDI($this->di);
0 ignored issues
show
Bug introduced by
The property tags2Questions does not seem to exist. Did you mean questions?

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...
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
27
        $this->users = new \Anax\Users\User();
0 ignored issues
show
Bug introduced by
The property users does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        $this->users->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
29
        $this->answers = new \Anax\Answers\CAnswers();
0 ignored issues
show
Bug introduced by
The property answers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
        $this->answers->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
31
        $this->comments = new \Anax\CommentDb\CommentsInDb();
0 ignored issues
show
Bug introduced by
The property comments does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        $this->comments->setDI($this->di);
0 ignored issues
show
Bug introduced by
It seems like $this->di can also be of type array or null; however, Anax\DI\TInjectable::setDI() does only seem to accept object<Anax\DI\class>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33
    }
34
35
    /**
36
     * Setup initial table for users.
37
     *
38
     * @return void
39
     */
40
    public function setupAction()
41
    {
42
        $this->questions->init();
43
        // $this->redirectTo('questions/');
44
    }
45
46
    /**
47
    * List recent questions.
48
     *
49
     * @return void
50
     */
51
    public function recentquestionsAction($count)
52
    {
53
        $this->views->add('default/page', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
54
            'title'     => 'Senaste frågorna',
55
            'content'     => 'Här hittar du de senast ställda frågorna',
56
        ]);
57
        $all = $this->questions->findRecent($count);
58
        foreach ($all as $question) {
59
            // TODO: Add more stuff into view? User, tags, ?
60
            // or shorten down and place in grid-box?
61
            // Add headline in view, "Senaste frågorna", or in index.php
62
            $this->views->add('questions/short', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
63
                'title'     => 'Senaste frågorna',
64
                'question'  => $question->getProperties(),
65
            ]);
66
        }
67
    }
68
69
    /**
70
     * List all questions.
71
     *
72
     * @return void
73
     */
74
    public function listAction($what = null, $id = null)
75
    {
76
        // Collect all questions in $questions array.
77
        $questions = array();
78
        switch ($what) {
79
            case 'tags':
80
                $tag = $this->tags->find($id)->getProperties();
81
                $title = "Kategori {$tag['name']}";
82
                $content = "Frågor i kategori {$tag['name']}";
83
                $qids = $this->tags2Questions->query()
0 ignored issues
show
Bug introduced by
The property tags2Questions does not seem to exist. Did you mean questions?

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...
84
                            ->where("tag_id = $id")
85
                            ->execute();
86
                foreach ($qids as $qid) {
87
                    $question = $this->questions->find($qid->question_id);
88
                    $questions[] = $question->getProperties();
89
                }
90
                break;
91
            case 'answers':
92
                $user = $this->users->query()
93
                ->where('acronym = ' . "'$id'")
94
                ->execute()[0];
95
                $answers = $this->answers->query()
96
                ->where('user_id = ' . "'$user->id'")
97
                ->execute();
98
                foreach ($answers as $answer) {
99
                    $questions[] =  $this->questions->query()
100
                    ->where('id = ' . "'$answer->q_id'")
101
                    ->execute()[0]->getProperties();
102
                }
103
                // Remove duplicate questions. E.g. same user answered multiple times on same question.
104
                $questions = array_unique($questions, SORT_REGULAR);
105
                $title = "Frågor svarade av {$user->name}";
106
                $content = "Frågor besvarade av {$user->name}";
107
                break;
108
            case 'questions':
109
                $user = $this->users->query()
110
                ->where('acronym = ' . "'$id'")
111
                ->execute()[0];
112
                $all = $this->questions->query()
113
                ->where('user_id = ' . "'$user->id'")
114
                ->execute();
115
                foreach ($all as $question) {
116
                    $questions[] = $question->getProperties();
117
                }
118
                $title = "Frågor från {$user->name}";
119
                $content = "Frågor ställda av {$user->name}";
120
                break;
121
122
            default:
123
                $title = "Alla frågor";
124
                $content = "Visa alla frågor";
125
                $all = $this->questions->findAll();
126
                foreach ($all as $question) {
127
                    $questions[] = $question->getProperties();
128
                }
129
                break;
130
        }
131
        // Display questions with tags and user.
132
        $this->theme->setTitle("Frågor");
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
133
        $this->views->add('default/page', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
134
            'title' => $title,
135
            'content' => $content,
136
        ]);
137
        foreach ($questions as $question) {
138
            $allTagsId = $this->tags2Questions->query()
0 ignored issues
show
Bug introduced by
The property tags2Questions does not seem to exist. Did you mean questions?

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...
139
            ->where('question_id = ' . "'{$question['id']}'")
140
            ->execute();
141
142
            // TODO: is below with $allTags needed?
143
            $allTags = array();
144
            foreach ($allTagsId as $tid) {
145
                $allTags[] = $this->tags->find($tid->tag_id)->getProperties();
146
            }
147
            $this->views->add('questions/short', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
148
                'question' => $question,
149
            ]);
150
            // Add view with the questions tags
151
            $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
152
                'controller' => 'tags',
153
                'action'     => 'question',
154
                'params'    => [$question['id'],  ],
155
            ]);
156
            // Add view with users card
157
            $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
158
                'controller' => 'users',
159
                'action'     => 'card',
160
                'params'    => [
161
                    $question['user_id'],
162
                ],
163
            ]);
164
        }
165
    }
166
167
    public function acceptAnswer($answer, $question)
168
    {
169
        if ($this->users->loggedIn() &&
170
            ($this->users->loggedInUser()->id == $question->user_id) &&
171
            !$answer->accepted &&
172
            !$question->accepted_answer) {
173
            $acceptUrl['href'] = $this->url->create('answers/accept/' . $answer->id);
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Coding Style Comprehensibility introduced by
$acceptUrl was never initialized. Although not strictly required by PHP, it is generally a good practice to add $acceptUrl = 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...
174
            $acceptUrl['text'] = '| Acceptera svar';
175
            $this->views->add('default/link', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
176
                'link' => $acceptUrl,
177
            ]);
178
        }
179
    }
180
181
    /**
182
     * Route to enable showing of comment or answer form
183
     * This is used when displaying single question
184
     *
185
     * @param int $form answer, comment_question, comment_answer
186
     *
187
     * @return void
188
     */
189
    public function showformAction($form)
190
    {
191
        $this->session->set('ShowFormCorA', $form);
0 ignored issues
show
Documentation introduced by
The property session does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
        $this->redirectTo($_SERVER['HTTP_REFERER']);
193
    }
194
195
    /**
196
     * Route to show link to or comment or answer form
197
     *
198
     * @param   string $form {answer, comment_question, comment_answer}
199
     *          string $controller to handle form
200
     *          int $id of question or answer
201
     *          string $linkText to display on linkText
202
     *          string 'q' or 'a' on comment on question or comment on answer
203
     *
204
     * @return void
205
     */
206
    private function showFormCommentOrAnswer($form, $controller, $id, $linkText, $qora = null)
207
    {
208
        // Add view to comment question
209
        if ($this->session->get('ShowFormCorA')==$form) {
0 ignored issues
show
Documentation introduced by
The property session does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
210
                $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
211
                    'controller' => $controller,
212
                    'action'     => 'add',
213
                    'params'    => [$id, $qora, ],
214
                ]);
215
        } else {
216
            if ($this->users->loggedIn()) {
217
                $route = "questions/showform/$form";
218
            } else {
219
                $route = 'users/login';
220
            }
221
            $commentFormUrl['href'] = $this->url->create($route);
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Coding Style Comprehensibility introduced by
$commentFormUrl was never initialized. Although not strictly required by PHP, it is generally a good practice to add $commentFormUrl = 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...
222
            $commentFormUrl['text'] = $linkText;
223
            $this->views->add('default/link', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
224
                'link' => $commentFormUrl,
225
            ]);
226
        }
227
    }
228
    /**
229
     * List single question
230
     *
231
     * @param int $id of question to display
232
     *
233
     * @return void
234
     */
235
    public function singleAction($id = null)
236
    {
237
        $question = $this->questions->query()
238
        ->where('id = ' . "'$id'")
239
        ->execute()[0];
240
241
        // Add view with the question.
242
        $this->theme->setTitle("$question->headline");
0 ignored issues
show
Documentation introduced by
The property theme does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
243
        $this->views->add('questions/single', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
244
            'question' => $question,
245
        ]);
246
247
        // Add view with the questions tags
248
        $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
249
            'controller' => 'tags',
250
            'action'     => 'question',
251
            'params'    => [$question->id,  ],
252
        ]);
253
254
        // Add view with user card.
255
        $humanTime = \Anax\CommentDb\CommentsInDb::humanTiming($question->created);
256
        $text = "Frågade för $humanTime sedan";
257
        $this->dispatcher->forward([
0 ignored issues
show
Documentation introduced by
The property dispatcher does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
258
            'controller' => 'users',
259
            'action'     => 'card',
260
            'params'    => [$question->user_id, 'q', $text, ],
261
        ]);
262
263
        // Add view to comment question, form visible when logged in and clicking link.
264
        $this->showFormCommentOrAnswer('comment_question', 'comment', $question->id, 'Kommentera ', 'q');
265
266
        // Add view to answer question, form visible when logged in and clicking link.
267
        $this->showFormCommentOrAnswer('answer', 'answers', $question->id, '| Besvara ');
268
269
        // Add view with Comments to question
270
        $allComments = $this->comments->query()
271
            ->where("q_or_a = 'q'")
272
            ->andwhere("q_or_a_id = $id")
273
            ->execute();
274
        $nrOfComments = sizeof($allComments);
275
        $commentsListData = array();
276
        for ($j=0; $j < $nrOfComments; $j++) {
277
            $commentsListData[$j]['comment'] = $allComments[$j]->getProperties();
278
            // Get user of comment
279
            $user = $this->users->find($allComments[$j]->user_id);
280
            $commentsListData[$j]['user'] = $user->getProperties();
281
        }
282
        $this->views->add('questions/comments', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
283
            'comments' => $commentsListData,
284
        ]);
285
286
        // Display all answers and comments to each answer.
287
        // Display all answers to question.
288
        $allAnswers = $this->answers->query()
289
            ->where("q_id = $question->id")
290
            ->execute();
291
        $this->views->add('questions/answersheading', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
292
            'title' => sizeof($allAnswers) . " svar",
293
        ]);
294
        foreach ($allAnswers as $answer) {
295
            $user = $this->users->find($answer->user_id);
296
            // Add view to display all answer.
297
            $this->views->add('questions/answer', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
298
                'answer' => $answer->getProperties(),
299
                'user'  => $user->getProperties(),
300
            ]);
301
            // Display link or comment form for commenting the answer
302
            $this->showFormCommentOrAnswer(
303
                'comment_answer_' . $answer->id,
304
                'comment',
305
                $answer->id,
306
                'Kommentera ',
307
                'a'
308
            );
309
            $this->acceptAnswer($answer, $question);
310
311
            // Get all comments to answer
312
            $allComments = $this->comments->query()
313
                ->where("q_or_a = 'a'")
314
                ->andwhere("q_or_a_id = $answer->id")
315
                ->execute();
316
            $commentsListData = array();
317
            foreach ($allComments as $comment) {
318
                $user = $this->users->find($comment->user_id);
319
                $commentsListData[] = [
320
                    'comment' => $comment->getProperties(),
321
                    'user' => $user->getProperties(),
322
                ];
323
            }
324
            $this->views->add('questions/comments', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
325
                'comments' => $commentsListData,
326
            ]);
327
        }
328
329
        $this->views->add('default/page', [
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
330
            'content'   => ' ',
331
            'links' => [
332
                [
333
                    'href' => $this->url->create('questions/ask'),
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
334
                    'text' => "Fråga en fråga",
335
                ],
336
            ],
337
        ]);
338
    }
339
340
    /**
341
     * Ask new question
342
     *
343
     * @param string $route to page for comment flow.
0 ignored issues
show
Bug introduced by
There is no parameter named $route. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
344
     *
345
     * @return void
346
     */
347 View Code Duplication
    public function askAction()
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...
348
    {
349
        if ($this->users->loggedIn()) {
350
            $this->di->session(); // Will load the session service which also starts the session
351
            $form = $this->createAddQuestionForm();
352
            $form->check([$this, 'callbackSuccess'], [$this, 'callbackFail']);
353
            $this->di->theme->setTitle("Fråga");
354
            $this->di->views->add('default/page', [
355
                'title' => "Ställ en fråga",
356
                'content' => $form->getHTML()
357
            ]);
358
        } else {
359
            $this->redirectTo($this->url->create('users/login'));
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
360
        }
361
    }
362
    private function createAddQuestionForm()
363
    {
364
        $tags = $this->tags->findAll();
365
        foreach ($tags as $tag) {
366
            $tagArray[] = $tag->name;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tagArray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tagArray = 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...
367
        }
368
        return $this->di->form->create([], [
369
            'headline' => [
370
                'type'        => 'text',
371
                'label'       => 'Rubrik:',
372
                'required'    => true,
373
                'validation'  => ['not_empty'],
374
            ],
375
            'content' => [
376
                'type'        => 'textarea',
377
                'label'       => 'Fråga:',
378
                'required'    => true,
379
                'validation'  => ['not_empty'],
380
            ],
381
            "tags" => [
382
                "type"        => "checkbox-multiple",
383
                'label'       => 'Kategorier:',
384
                "values"      => $tagArray,
0 ignored issues
show
Bug introduced by
The variable $tagArray 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...
385
            ],
386
            'submit' => [
387
                'type'      => 'submit',
388
                'callback'  => [$this, 'callbackSubmitAddQuestion'],
389
            ],
390
            // 'submit-fail' => [
391
            //     'type'      => 'submit',
392
            //     'callback'  => [$this, 'callbackSubmitFailAddQuestion'],
393
            // ],
394
        ]);
395
    }
396
    /**
397
     * Callback for submit-button.
398
     *
399
     */
400
    public function callbackSubmitAddQuestion($form)
401
    {
402
        // $form->AddOutput("<p>DoSubmit(): Form was submitted.<p>");
403
        // $form->AddOutput("<p>Do stuff (save to database) and return true (success) or false (failed processing)</p>");
404
405
        // Save comment to database
406
        $now = time();
407
        $this->questions->save([
408
            'headline' => $form->Value('headline'),
409
            'content' => $form->Value('content'),
410
            'user_id' => $this->users->loggedInUser()->id,
411
            'vote' => 0,
412
            'created' => $now,
413
        ]);
414
        // Pull out tags and store them in tags2question model
415
        $qid = $this->questions->id;
416
        $tags = $form->Value('tags');
417
        foreach ($tags as $tag) {
418
            $tag_id = $this->tags->findTag($tag)->getProperties()['id'];
419
            unset($this->tags2Questions->id);
420
            $this->tags2Questions->save([
0 ignored issues
show
Bug introduced by
The property tags2Questions does not seem to exist. Did you mean questions?

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...
421
                'tag_id' => $tag_id,
422
                'question_id' => $qid,
423
            ]);
424
        }
425
426
        // $form->AddOutput("<p><b>Name: " . $form->Value('name') . "</b></p>");
427
        // $form->AddOutput("<p><b>Email: " . $form->Value('mail') . "</b></p>");
428
        $form->saveInSession = false;
429
        return true;
430
    }
431
    /**
432
     * Callback for submit-button.
433
     *
434
     */
435
    public function callbackSubmitFailAddQuestion($form)
436
    {
437
        // TODO: Remove this?
438
        $form->AddOutput("<p><i>DoSubmitFail(): Form was submitted but I failed to process/save/validate it</i></p>");
439
        return false;
440
    }
441
    /**
442
     * Callback What to do if the form was submitted?
443
     *
444
     */
445
    public function callbackSuccess($form)
446
    {
447
        $form->AddOUtput("<p><i>Form was submitted and the callback method returned true.</i></p>");
448
        // Redirect to page displaying the new question.
449
        $this->redirectTo($this->url->create('questions/single/' . $this->questions->id));
0 ignored issues
show
Documentation introduced by
The property url does not exist on object<Anax\Questions\QuestionsController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
450
    }
451
    /**
452
     * Callback What to do when form could not be processed?
453
     *
454
     */
455
    public function callbackFail($form)
456
    {
457
        $form->AddOutput("<p><i>Form was submitted and the Check() method returned false.</i></p>");
458
        // Redirect to comment form.
459
        $this->redirectTo();
460
    }
461
}
462