Completed
Push — master ( 0051a3...d0941c )
by Richard
18s
created

XoopsCaptchaText   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 7.89 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
dl 3
loc 38
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 3 11 2
A loadText() 0 13 2

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * CAPTCHA for text mode
14
 *
15
 * PHP 5.3
16
 *
17
 * @category  Xoops\Class\Captcha\Captcha
18
 * @package   Captcha
19
 * @author    Taiwen Jiang <[email protected]>
20
 * @copyright 2013 XOOPS Project (http://xoops.org)
21
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22
 * @version   $Id$
23
 * @link      http://xoops.org
24
 * @since     2.6.0
25
 */
26
class XoopsCaptchaText extends XoopsCaptchaMethod
27
{
28
    /**
29
     * XoopsCaptchaText::render()
30
     *
31
     * @return string
32
     */
33 1
    public function render()
34
    {
35 1
        $form = $this->loadText() . '&nbsp;&nbsp; <input type="text" name="' . $this->config['name']
36 1
			. '" id="' . $this->config['name'] . '" size="' . $this->config['num_chars']
37 1
			. '" maxlength="' . $this->config['num_chars'] . '" value="" />';
38 1
        $form .= '<br />' . XoopsLocale::INPUT_RESULT_FROM_EXPRESSION;
39 1 View Code Duplication
        if (!empty($this->config['maxattempts'])) {
40
            $form .= '<br />' . sprintf(XoopsLocale::F_MAXIMUM_ATTEMPTS, $this->config['maxattempts']);
41
        }
42 1
        return $form;
43
    }
44
45
    /**
46
     * XoopsCaptchaText::loadText()
47
     *
48
     * @return string
49
     */
50 2
    public function loadText()
51
    {
52 2
        $val_a = mt_rand(0, 9);
53 2
        $val_b = mt_rand(0, 9);
54 2
        if ($val_a > $val_b) {
55
            $expression = "{$val_a} - {$val_b} = ?";
56
            $this->code = $val_a - $val_b;
0 ignored issues
show
Documentation Bug introduced by
The property $code was declared of type string, but $val_a - $val_b is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
57
        } else {
58 2
            $expression = "{$val_a} + {$val_b} = ?";
59 2
            $this->code = $val_a + $val_b;
0 ignored issues
show
Documentation Bug introduced by
The property $code was declared of type string, but $val_a + $val_b is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
60
        }
61 2
        return '<span style="font-style: normal; font-weight: bold; font-size: 100%; font-color: #333; border: 1px solid #333; padding: 1px 5px;">' . $expression . '</span>';
62
    }
63
}
64