Passed
Push — master ( ea5d4f...e4e351 )
by Magnus
02:01
created

CreatePostForm   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 136
Duplicated Lines 22.79 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 23.38%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 6
dl 31
loc 136
ccs 18
cts 77
cp 0.2338
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 31 31 1
B callbackSubmit() 0 39 2
C createCategory() 0 48 7

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 Radchasay\Comment\HTMLForm;
4
5
use \Anax\HTMLForm\FormModel;
6
use \Anax\DI\DIInterface;
7
use \Radchasay\Comment\Post;
8
use \Radchasay\User\User;
9
use \Radchasay\Comment\PostCategory;
10
use \Radchasay\Comment\Post2Cat;
11
12
/**
13
 * Example of FormModel implementation.
14
 */
15
class CreatePostForm extends FormModel
16
{
17
    /**
18
     * Constructor injects with DI container.
19
     *
20
     * @param Anax\DI\DIInterface $di a service container
21
     */
22 2 View Code Duplication
    public function __construct(DIInterface $di)
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...
23
    {
24 2
        parent::__construct($di);
25
26 2
        $this->form->create(
27
            [
28 2
                "id"     => __CLASS__,
29 2
                "legend" => "Create Post",
30
                "class" => "createNewPostForm"
31 2
            ],
32
            [
33
                "title" => [
34 2
                    "type" => "text",
35 2
                ],
36
37
                "text" => [
38 2
                    "type" => "textarea",
39 2
                ],
40
41
                "tags" => [
42 2
                    "type"        => "text",
43 2
                ],
44
45
                "submit" => [
46 2
                    "type"     => "submit",
47 2
                    "value"    => "Create Post",
48 2
                    "callback" => [$this, "callbackSubmit"],
49 2
                ],
50
            ]
51 2
        );
52 2
    }
53
54
55
    /**
56
     * Callback for submit-button which should return true if it could
57
     * carry out its work and false if something failed.
58
     *
59
     * @return boolean true if okey, false if something went wrong.
60
     */
61
    public function callbackSubmit()
62
    {
63
        // Get values from the submitted form
64
        //
65
        $post = new Post();
66
        $post->setDB($this->di->get("db"));
67
        $title = htmlentities($this->form->value("title"));
68
        $data = $this->form->value("text");
69
        $text = $this->di->get("textfilter")->doFilter($data, ["shortcode", "markdown", "clickable", "bbcode"]);
70
71
        var_dump(strlen($title));
0 ignored issues
show
Security Debugging Code introduced by
var_dump(strlen($title)); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
72
        if (strlen($title) > 50 ) {
73
            //$this->form->remeberValues();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
            $this->form->addOutput("Title is too long. Please fix!");
75
            return false;
76
        } else {
77
            $post->posttitle = $title;
78
        }
79
80
        $post->posttext = $text;
81
        $post->postname = htmlentities($this->di->get("session")->get("email"));
82
83
        $tags = htmlspecialchars($this->form->value("tags"));
84
85
        $user = new User();
86
        $user->setDb($this->di->get("db"));
87
        $user->getInformation($post->postname);
88
        $user->points += 2;
0 ignored issues
show
Bug introduced by
The property points does not seem to exist in Radchasay\User\User.

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...
89
90
        $user->save();
91
        $post->save();
92
93
94
        $this->createCategory($tags, $post->id);
95
96
        $url = $this->di->get("url")->create("comment/viewAllPosts");
97
        $this->di->get("response")->redirect($url);
98
        return true;
99
    }
100
101
102
    public function createCategory($tags, $postId)
103
    {
104
        $postcat = new PostCategory();
105
        $postcat->setDb($this->di->get("db"));
106
        $allCats = $postcat->findall();
107
108
        $existingCats = [];
109
110
        foreach ($allCats as $ac) {
111
            array_push($existingCats, $ac->category);
112
        }
113
114
        $tags = array_map('trim', explode(',', $tags));
115
116
        $tags = array_map("strtoupper", $tags);
117
118
        $uniqueTags = [];
119
120
        foreach ($tags as $t) {
121
            if (!in_array($t, $existingCats)) {
122
                if (!in_array($t, $uniqueTags)) {
123
                    array_push($uniqueTags, $t);
124
                }
125
            }
126
        }
127
128
        foreach ($uniqueTags as $ut) {
129
            $pc = new PostCategory();
130
            $pc->setDb($this->di->get("db"));
131
            $pc->category = $ut;
132
133
            $pc->save();
134
        }
135
        //
136
        foreach ($tags as $t) {
137
            $pc = new PostCategory();
138
            $pc->setDb($this->di->get("db"));
139
140
            $id = $pc->getId($t);
141
142
            $p2c = new Post2Cat();
143
            $p2c->setDb($this->di->get("db"));
144
            $p2c->catid = $id;
145
            $p2c->postid = $postId;
146
147
            $p2c->save();
148
        }
149
    }
150
}
151