Completed
Branch master (a6293b)
by Patrick
06:54 queued 14s
created

FlipsideCAPTCHA   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 128
Duplicated Lines 25.78 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 3 Features 0
Metric Value
c 4
b 3
f 0
dl 33
loc 128
rs 10
wmc 20
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A get_valid_captcha_ids() 0 12 2
A get_all() 0 13 2
A __construct() 0 6 1
A get_question() 11 11 2
A jsonSerialize() 0 9 1
A save_new_captcha() 0 6 1
A get_hint() 11 11 2
A get_answer() 11 11 2
A is_answer_right() 0 4 1
A self_json_encode() 0 4 1
B draw_captcha() 0 25 5

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
require_once('Autoload.php');
3
class FlipsideCAPTCHA implements JsonSerializable
4
{
5
    public  $random_id;
6
    private $validIDs;
7
8
    public static function get_valid_captcha_ids()
9
    {
10
        $dataset = DataSetFactory::getDataSetByName('profiles');
11
        $datatable = $dataset['captcha'];
12
        $data = $datatable->read(false, array('id'));
13
        $count = count($data);
14
        for($i = 0; $i < $count; $i++)
15
        {
16
            $data[$i] = $data[$i]['id'];
17
        }
18
        return $data;
19
    }
20
21
    public static function get_all()
22
    {
23
        $res = array();
24
        $ids = FlipsideCAPTCHA::get_valid_captcha_ids();
25
        $count = count($ids);
26
        for($i = 0; $i < $count; $i++)
27
        {
28
            $captcha = new FlipsideCAPTCHA();
29
            $captcha->random_id = $ids[$i]; 
30
            array_push($res, $captcha);
31
        }
32
        return $res;
33
    }
34
35
    public static function save_new_captcha($question, $hint, $answer)
36
    {
37
        $dataset = DataSetFactory::getDataSetByName('profiles');
38
        $datatable = $dataset['captcha'];
39
        return $datatable->create(array('question'=>$question, 'hint'=>$hint, 'answer'=>$answer));
40
    }
41
42
    public function __construct()
43
    {
44
        $this->validIDs = FlipsideCAPTCHA::get_valid_captcha_ids();
45
        $this->random_id = mt_rand(0, count($this->validIDs) - 1);
46
        $this->random_id = $this->validIDs[$this->random_id];
47
    }
48
49 View Code Duplication
    public function get_question()
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...
50
    {
51
        $dataset = DataSetFactory::getDataSetByName('profiles');
52
        $datatable = $dataset['captcha'];
53
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('question'));
54
        if($data === false)
55
        {
56
            return false;
57
        }
58
        return $data[0]['question'];
59
    }
60
61 View Code Duplication
    public function get_hint()
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...
62
    {
63
        $dataset = DataSetFactory::getDataSetByName('profiles');
64
        $datatable = $dataset['captcha'];
65
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('hint'));
66
        if($data === false)
67
        {
68
            return false;
69
        }
70
        return $data[0]['hint'];
71
    }
72
73 View Code Duplication
    private function get_answer()
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...
74
    {
75
        $dataset = DataSetFactory::getDataSetByName('profiles');
76
        $datatable = $dataset['captcha'];
77
        $data = $datatable->read(new \Data\Filter('id eq '.$this->random_id), array('answer'));
78
        if($data === false)
79
        {
80
            return false;
81
        }
82
        return $data[0]['answer'];
83
    }
84
85
    public function is_answer_right($answer)
86
    {
87
        return strcasecmp($this->get_answer(), $answer) === 0;
88
    }
89
90
    public function draw_captcha($explination = true, $return = false, $ownForm = false)
91
    {
92
        $string = '';
93
94
        if($ownForm)
95
        {
96
            $string .= '<form id="flipcaptcha" name="flipcaptcha">';
97
        }
98
99
        $string .= '<label for="captcha" class="col-sm-2 control-label">'.$this->get_question().'</label><div class="col-sm-10"><input class="form-control" type="text" id="captcha" name="captcha" placeholder="'.$this->get_hint().'" required/></div>';
100
        if($ownForm)
101
        {
102
            $string .= '</form>';
103
        }
104
        if($explination)
105
        {
106
            $string .= '<div class="col-sm-10">The answer to this question may be found in the Burning Flipside Survival Guide. It may be found <a href="http://www.burningflipside.com/sg">here</a>.</div>';
107
        }
108
        
109
        if(!$return)
110
        {
111
            echo $string;
112
        }
113
        return $string;
114
    }
115
116
    public function self_json_encode()
117
    {
118
        return json_encode($this->jsonSerialize());
119
    }
120
121
    public function jsonSerialize()
122
    {
123
        $res = array();
124
        $res['id'] = $this->random_id;
125
        $res['question'] = $this->get_question();
126
        $res['hint'] = $this->get_hint();
127
        $res['answer'] = $this->get_answer();
128
        return $res;
129
    }
130
}
131
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
132