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.

FocalLength::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
/**
3
 * PHP Exif FocalLength ValueObject
4
 *
5
 * @link        http://github.com/PHPExif/php-exif-common for the canonical source repository
6
 * @copyright   Copyright (c) 2016 Tom Van Herreweghe <[email protected]>
7
 * @license     http://github.com/PHPExif/php-exif-common/blob/master/LICENSE MIT License
8
 * @category    PHPExif
9
 * @package     Common
10
 */
11
12
namespace PHPExif\Common\Data\ValueObject;
13
14
use PHPExif\Common\Data\ValueObject\StringObject;
15
use \InvalidArgumentException;
16
use \RuntimeException;
17
18
/**
19
 * FocalLength class
20
 *
21
 * A value object to describe the FocalLength data
22
 *
23
 * @category    PHPExif
24
 * @package     Common
25
 */
26
class FocalLength extends StringObject
27
{
28
    /**
29
     * @param string $stringData
30
     *
31
     * @throws InvalidArgumentException If given argument is not a string
32
     */
33 View Code Duplication
    public function __construct($stringData)
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...
34
    {
35
        if (!is_string($stringData)) {
36
            throw new InvalidArgumentException('Given data is not a string');
37
        }
38
39
        if (!preg_match('#^([0-9]+)/([0-9]+)$#', $stringData, $matches)) {
40
            throw new RuntimeException('Given focal length is not in a valid format. Need: "<number>/<number>"');
41
        }
42
43
        $numerator = (int) $matches[1];
44
        $denominator = (int) $matches[2];
45
46
        // normalize:
47
        $numerator /= $denominator;
48
49
        $this->setStringData("{$numerator}mm");
50
    }
51
52
    /**
53
     * Creates new instance with data in milimeter
54
     *
55
     * @param string $stringData
56
     *
57
     * @return FocalLength
58
     */
59
    public static function fromMM($stringData)
60
    {
61
        if (!is_string($stringData)) {
62
            throw new InvalidArgumentException('Given data is not a string');
63
        }
64
65
        if (!preg_match('#^([0-9]+)mm$#', $stringData, $matches)) {
66
            throw new RuntimeException('Given focal length is not in a valid format. Need: "<number>mm"');
67
        }
68
69
        $numerator = $matches[1];
70
71
        return new self(
72
            "{$numerator}/1"
73
        );
74
    }
75
76
    /**
77
     * Creates a new instance from given FocalLength object
78
     *
79
     * @param FocalLength $focalLength
80
     *
81
     * @return FocalLength
82
     */
83
    public static function fromFocalLength(FocalLength $focalLength)
84
    {
85
        return self::fromMM($focalLength->getStringData());
86
    }
87
}
88