GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Builder   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setBirthday() 0 3 1
A setGender() 0 6 3
A setSerialNumber() 0 2 1
A getNumber() 0 15 2
A getParser() 0 3 1
A setParser() 0 4 1
A checkBuilderFields() 0 9 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace SLWDC\NICParser;
5
6
7
use SLWDC\NICParser\Exception\BadMethodCallException;
8
use SLWDC\NICParser\Exception\InvalidArgumentException;
9
10
class Builder {
11
12
  /**
13
   * @var \DateTime
14
   */
15
  private $birthday;
16
17
  /**
18
   * @var string
19
   */
20
  private $gender;
21
22
  /**
23
   * @var int
24
   */
25
  private $serial_number;
26
27
  public function setParser(Parser $parser) {
28
    $this->birthday = $parser->getBirthday();
29
    $this->gender = $parser->getGender();
30
    $this->serial_number = $parser->getSerialNumber();
31
  }
32
33
  public function setBirthday(\DateTime $date) {
34
    $this->birthday = clone $date;
35
    return $this;
36
  }
37
38
  public function setGender(string $gender = 'M') {
39
    if ($gender === 'M' || $gender === 'F') {
40
      $this->gender = $gender;
41
      return $this;
42
    }
43
    throw new InvalidArgumentException('Unknown gender. Allowed values are: "M" and "F');
44
  }
45
46
  public function setSerialNumber(int $serial_number) {
47
    $this->serial_number = $serial_number;
48
  }
49
50
  public function getNumber(): string {
51
    $this->checkBuilderFields();
52
53
    $year = $this->birthday->format('Y');
54
    $start_date = (new \DateTime())->setDate((int) $year, 1, 1)->setTime(0, 0);
55
    $birth_date_count = (int) $this->birthday->diff($start_date)->format('%a');
0 ignored issues
show
Bug introduced by
It seems like $start_date can also be of type false; however, parameter $datetime2 of DateTime::diff() does only seem to accept DateTimeInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
    $birth_date_count = (int) $this->birthday->diff(/** @scrutinizer ignore-type */ $start_date)->format('%a');
Loading history...
56
57
    ++$birth_date_count;
58
59
    if ($this->gender === 'F') {
60
      $birth_date_count += 500;
61
    }
62
63
    $serial = $this->serial_number;
64
    return "{$year}{$birth_date_count}{$serial}";
65
  }
66
67
  public function getParser(): Parser {
68
    $number = $this->getNumber();
69
    return new Parser($number);
70
  }
71
72
  public function checkBuilderFields() {
73
    if (!$this->birthday) {
74
      throw new BadMethodCallException('Attempting to build ID number without a valid birthday set.');
75
    }
76
    if (!$this->gender) {
77
      throw new BadMethodCallException('Attempting to build ID number without a valid gender set.');
78
    }
79
    if (!$this->serial_number) {
80
      throw new BadMethodCallException('Attempting to build ID number without a valid serial number set.');
81
    }
82
  }
83
}
84