Passed
Push — master ( c32af1...923c61 )
by ANTHONIUS
04:29
created

Prompter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 16
dl 0
loc 45
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Fake;
15
16
use PhpSpec\Console\Prompter as PrompterInterface;
17
18
class Prompter implements PrompterInterface
19
{
20
    private $answers      = [];
21
    private $hasBeenAsked = false;
22
    private $question;
23
    private $unansweredQuestions = false;
24
25
    public function setAnswer($answer)
26
    {
27
        $this->answers[] = $answer;
28
    }
29
30
    public function askConfirmation(string $question, bool $default = true): bool
31
    {
32
        $this->hasBeenAsked = true;
33
        $this->question     = $question;
34
35
        $this->unansweredQuestions = \count($this->answers) > 1;
36
37
        return (bool) array_shift($this->answers);
38
    }
39
40
    public function hasBeenAsked($question = null)
41
    {
42
        if (!$question) {
43
            return $this->hasBeenAsked;
44
        }
45
46
        return $this->hasBeenAsked
47
            && $this->normalise($this->question) === $this->normalise($question);
48
    }
49
50
    public function hasUnansweredQuestions()
51
    {
52
        return $this->unansweredQuestions;
53
    }
54
55
    /**
56
     * @param mixed $question
57
     *
58
     * @return mixed
59
     */
60
    private function normalise($question)
61
    {
62
        return preg_replace('/\s+/', '', trim(strip_tags($question)));
63
    }
64
}
65