Completed
Push — master ( 7bebed...2fd43b )
by Lars
01:41
created

ValidatorResult::setError()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.1502

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 9
cts 11
cp 0.8182
rs 8.5906
cc 5
eloc 13
nc 8
nop 2
crap 5.1502
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\HtmlFormValidator;
6
7
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
8
use voku\helper\HtmlDomParser;
9
10
class ValidatorResult
11
{
12
  /**
13
   * List of errors from validation
14
   *
15
   * @var string[]
16
   */
17
  private $errors = [];
18
19
  /**
20
   * @var array
21
   */
22
  private $values = [];
23
24
  /**
25
   * @var HtmlDomParser
26
   */
27
  private $formDocument;
28
29
  /**
30
   * ValidatorResult constructor.
31
   *
32
   * @param HtmlDomParser $formDocument
33
   */
34 19
  public function __construct($formDocument)
35
  {
36 19
    $this->formDocument = clone $formDocument;
37 19
  }
38
39
  /**
40
   * @return int
41
   */
42 7
  public function countErrors(): int
43
  {
44 7
    return \count($this->errors);
45
  }
46
47
  /**
48
   * @return array
49
   */
50 13
  public function getErrorMessages(): array
51
  {
52 13
    return $this->errors;
53
  }
54
55
  /**
56
   * @return string
57
   */
58 2
  public function getHtml(): string
59
  {
60 2
    return $this->formDocument->html();
61
  }
62
63
  /**
64
   * @return array
65
   */
66 2
  public function getValues(): array
67
  {
68 2
    return $this->values;
69
  }
70
71
  /**
72
   * @return bool
73
   */
74 7
  public function isSuccess(): bool
75
  {
76 7
    return ($this->countErrors() === 0);
77
  }
78
79
  /**
80
   * @param string          $field
81
   * @param string|string[] $errorMsg
82
   *
83
   * @return ValidatorResult
84
   */
85 12
  public function setError(string $field, $errorMsg): ValidatorResult
86
  {
87
    try {
88 12
      $inputTag = $this->formDocument->find('[name=' . $field . ']', 0);
89 3
    } catch (SyntaxErrorException $syntaxErrorException) {
90 3
      $inputTag = null;
91
      // TODO@me -> can the symfony CssSelectorConverter use array-name-attributes?
92
    }
93
94 12
    if ($inputTag) {
95 9
      $inputTag->setAttribute('aria-invalid', 'true');
0 ignored issues
show
Bug introduced by
The method setAttribute does only exist in voku\helper\SimpleHtmlDom, but not in voku\helper\SimpleHtmlDomNodeInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
96
    }
97
98 12
    if (\is_array($errorMsg) === true) {
99
      foreach ($errorMsg as $errorMsgSingle) {
100
        $this->errors[$field][] = $errorMsgSingle;
101
      }
102
    } else {
103 12
      $this->errors[$field][] = $errorMsg;
104
    }
105
106 12
    return $this;
107
  }
108
109 18
  public function setValues(string $field, $value): ValidatorResult
110
  {
111 18
    $this->values[$field] = $value;
112
113 18
    return $this;
114
  }
115
}
116