Passed
Push — master ( 487089...443b76 )
by Chris
03:30
created

datetimeinterface_to_datetimeimmutable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.576

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
ccs 3
cts 5
cp 0.6
crap 3.576
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace DaveRandom\LibLifxLan;
4
5
if (\strlen(\pack('e', 0.1)) === 4) {
6
    \define(__NAMESPACE__ . '\\FLOAT32_CODE', 'e');
7
} else if (\strlen(\pack('g', 0.1)) === 4) {
8
    \define(__NAMESPACE__ . '\\FLOAT32_CODE', 'g');
9
} else {
10
    /** @noinspection PhpUnhandledExceptionInspection */
11
    throw new \Error('Cannot pack()/unpack() floating point numbers to a 32-bit little-endian representation');
12
}
13
14
const UINT32_MIN = \PHP_INT_SIZE === 4 ? \PHP_INT_MIN : 0;
15
const UINT32_MAX = \PHP_INT_SIZE === 4 ? \PHP_INT_MAX : 0xffffffff;
16
17
function datetimeinterface_to_datetimeimmutable(\DateTimeInterface $dateTime): \DateTimeImmutable
18
{
19 6
    if ($dateTime instanceof \DateTimeImmutable) {
20
        return $dateTime;
21
    }
22
23 6
    if ($dateTime instanceof \DateTime) {
24 6
        return \DateTimeImmutable::createFromMutable($dateTime);
25
    }
26
27
    return \DateTimeImmutable::createFromFormat('u U', $dateTime->format('u U'));
0 ignored issues
show
Bug introduced by
The call to DateTimeImmutable::createFromFormat() has too few arguments starting with timezone. ( Ignorable by Annotation )

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

27
    return \DateTimeImmutable::/** @scrutinizer ignore-call */ createFromFormat('u U', $dateTime->format('u U'));

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug Best Practice introduced by
The expression return DateTimeImmutable...ateTime->format('u U')) could return the type false which is incompatible with the type-hinted return DateTimeImmutable. Consider adding an additional type-check to rule them out.
Loading history...
28
}
29