Completed
Push — master ( 206c33...9e77c2 )
by Oscar
05:44
created

Honeypot   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 2
cbo 5
dl 18
loc 84
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A inputName() 0 6 1
A inputClass() 0 6 1
B __invoke() 18 18 5
A isValid() 0 6 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
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
6
use Psr7Middlewares\Utils;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Middleware to span protection using the honeypot technique.
12
 */
13
class Honeypot
14
{
15
    use Utils\FormTrait;
16
17
    /**
18
     * @var string The honeypot input name
19
     */
20
    protected $inputName = 'hpt_name';
21
22
    /**
23
     * @var string The honeypot class name
24
     */
25
    protected $inputClass = 'hpt_input';
26
27
    /**
28
     * Set the field name.
29
     * 
30
     * @param string $inputName
31
     * 
32
     * @return self
33
     */
34
    public function inputName($inputName)
35
    {
36
        $this->inputName = $inputName;
37
38
        return $this;
39
    }
40
41
    /**
42
     * Set the field class.
43
     * 
44
     * @param string $inputClass
45
     * 
46
     * @return self
47
     */
48
    public function inputClass($inputClass)
49
    {
50
        $this->inputClass = $inputClass;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Execute the middleware.
57
     *
58
     * @param ServerRequestInterface $request
59
     * @param ResponseInterface      $response
60
     * @param callable               $next
61
     *
62
     * @return ResponseInterface
63
     */
64 View Code Duplication
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
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...
65
    {
66
        if (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) {
67
            throw new RuntimeException('Honeypot middleware needs FormatNegotiator executed before');
68
        }
69
70
        if (FormatNegotiator::getFormat($request) !== 'html') {
71
            return $next($request, $response);
72
        }
73
74
        if ($this->isPost($request) && !$this->isValid($request)) {
75
            return $response->withStatus(403);
76
        }
77
78
        $response = $next($request, $response);
79
80
        return $this->insertIntoPostForms($response, '<input type="text" name="'.$this->inputName.'" class="'.$this->inputClass.'">');
81
    }
82
83
    /**
84
     * Check whether the request is valid.
85
     * 
86
     * @param ServerRequestInterface $request
87
     * 
88
     * @return bool
89
     */
90
    protected function isValid(ServerRequestInterface $request)
91
    {
92
        $data = $request->getParsedBody();
93
94
        return isset($data[$this->inputName]) && $data[$this->inputName] === '';
95
    }
96
}
97