Completed
Pull Request — master (#44)
by Nate
04:08 queued 02:15
created

FloatTypeAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 34
ccs 10
cts 10
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 9 2
A read() 0 8 2
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson;
10
11
/**
12
 * Class FloatTypeAdapter
13
 *
14
 * @author Nate Brunette <[email protected]>
15
 */
16
class FloatTypeAdapter extends TypeAdapter
17
{
18
    /**
19
     * Read the next value, convert it to its type and return it
20
     *
21
     * @param JsonReadable $reader
22
     * @return float|null
23
     */
24
    public function read(JsonReadable $reader): ?float
25
    {
26
        if ($reader->peek() === JsonToken::NULL) {
27
            $reader->nextNull();
28
            return null;
29 3
        }
30
31 3
        return $reader->nextDouble();
32 1
    }
33 1
34
    /**
35
     * Write the value to the writer for the type
36 2
     *
37
     * @param JsonWritable $writer
38
     * @param float|null $value
39
     * @return void
40
     */
41
    public function write(JsonWritable $writer, $value): void
42
    {
43
        if (null === $value) {
44
            $writer->writeNull();
45
46 3
            return;
47
        }
48 3
49 1
        $writer->writeFloat((float)$value);
50
    }
51
}
52