Passed
Push — extents ( f35511...ba1d5f )
by Doug
61:44
created

NADCON5Grid::getAdjustment()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 77
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 8.1373

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 30
c 1
b 0
f 0
nc 128
nop 2
dl 0
loc 77
ccs 27
cts 31
cp 0.871
crap 8.1373
rs 8.0088

2 Methods

Rating   Name   Duplication   Size   Complexity  
A NADCON5Grid::getValues() 0 7 2
A NADCON5Grid::getHeader() 0 7 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\CoordinateOperation;
10
11
use SplFileObject;
12
use UnexpectedValueException;
13
use function unpack;
14
15
class NADCON5Grid extends Grid
16
{
17
    use BiquadraticInterpolation;
18
19
    private string $gridDataType;
20
21
    public function __construct($filename)
22
    {
23
        $this->gridFile = new SplFileObject($filename);
24
25
        $header = $this->getHeader();
26 36
        $this->startX = $header['xlonsw'];
27
        $this->startY = $header['xlatsw'];
28 36
        $this->columnGridInterval = $header['dlon'];
29
        $this->rowGridInterval = $header['dlat'];
30 36
        $this->numberOfColumns = $header['nlon'];
31 36
        $this->numberOfRows = $header['nlat'];
32 36
33 36
        $this->gridDataType = match ($header['ikind']) {
34 36
            1 => 'G',
35 36
            default => throw new UnexpectedValueException("Unknown ikind: {$header['ikind']}"),
36 36
        };
37
    }
38 36
39 36
    protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues
40 36
    {
41 36
        $startBytes = 52;
42
        $dataTypeBytes = $this->gridDataType === 'n' ? 2 : 4;
43
        $recordLength = 4 + $this->numberOfColumns * $dataTypeBytes + 4;
44
45 36
        $this->gridFile->fseek($startBytes + $recordLength * $latitudeIndex);
46
        $rawRow = $this->gridFile->fread($recordLength);
47 36
        $row = unpack("Gstartbuffer/{$this->gridDataType}{$this->numberOfColumns}lon/Gendbuffer", $rawRow);
48
49 36
        return new GridValues(
50
            $longitudeIndex * $this->columnGridInterval + $this->startX,
51
            $latitudeIndex * $this->rowGridInterval + $this->startY,
52
            [$row['lon' . ($longitudeIndex + 1)]]
53
        );
54
    }
55 36
56
    private function getHeader(): array
57 36
    {
58 31
        $this->gridFile->fseek(0);
59
        $rawData = $this->gridFile->fread(52);
60
        $data = unpack('Gstartbuffer/Exlatsw/Exlonsw/Edlat/Edlon/Nnlat/Nnlon/Nikind/Gendbuffer/', $rawData);
61
62
        return $data;
63
    }
64
65
    public function getValues(float $x, float $y): array
66
    {
67
        if ($x < 0) {
68
            $x += 360; // NADCON5 uses 0 = 360 = Greenwich
69 36
        }
70 36
71 36
        return $this->interpolate($x, $y);
72 36
    }
73
}
74