ValueNotValid::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 3
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 9/15/17
7
 * Time: 11:18 PM
8
 */
9
10
namespace Tfboe\FmLib\Exceptions;
11
12
/**
13
 * Class AuthenticationException
14
 * @package Tfboe\FmLib\Exceptions
15
 */
16
class ValueNotValid extends AbstractException
17
{
18
//<editor-fold desc="Constructor">
19
  /**
20
   * @param mixed $value
21
   * @param string|null $enumName the corresponding enum class name or null
22
   * @param string $valueFunction the function to call on the enum class to get the list of possible values
23
   */
24
  public function __construct($value, ?string $enumName = null, string $valueFunction = "getValues")
25
  {
26
    $message = "The following value is not valid: " . json_encode($value);
27
    if ($enumName != null && method_exists($enumName, $valueFunction)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $enumName of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
28
      /** @var mixed[] $values */
29
      $values = call_user_func([$enumName, $valueFunction]);
30
      assert(!in_array($value, $values, true));
31
      $message .= " in " . $enumName . ". Possible values: " .
32
        implode(', ', array_map("json_encode", $values)) . ".";
33
    }
34
    parent::__construct($message);
35
  }
36
//</editor-fold desc="Constructor">
37
}