Completed
Pull Request — master (#3)
by Hannes
02:16
created

SRTM4Provider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 7.45 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 7
loc 94
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initResourceReader() 0 4 1
A setFilenameFormat() 0 4 1
A getFilenameFor() 0 6 1
A loadTileReferencesFor() 0 21 4
A getExactRowAndColFor() 7 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Runalyze DEM Reader.
5
 *
6
 * (c) RUNALYZE <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Runalyze\DEM\Provider\GeoTIFF;
13
14
use Runalyze\DEM\Exception\InvalidArgumentException;
15
16
class SRTM4Provider extends AbstractGeoTIFFProvider
17
{
18
    /** @var int */
19
    const MAX_LATITUDE = 60;
20
21
    /** @var int */
22
    const MAX_LONGITUDE = 180;
23
24
    /** @var float */
25
    const DEGREES_PER_TILE = 5.0;
26
27
    /** @var float */
28
    protected $CurrentTileHorizontalReference;
29
30
    /** @var float */
31
    protected $CurrentTileVerticalReference;
32
33
    /** @var float top left latitude */
34
    protected $CurrentTileLatitude;
35
36
    /** @var float top left longitude */
37
    protected $CurrentTileLongitude;
38
39
    /** @var string */
40
    protected $FilenameFormat = 'srtm_%02d_%02d.tif';
41
42
    /** @var GeoTIFFReader */
43
    protected $ResourceReader;
44
45
    public function initResourceReader()
46
    {
47
        $this->ResourceReader = new GeoTIFFReader();
48
    }
49
50
    /**
51
     * @param $format
52
     */
53
    public function setFilenameFormat($format)
54
    {
55
        $this->FilenameFormat = $format;
56
    }
57
58
    /**
59
     * @param  float                    $latitude
60
     * @param  float                    $longitude
61
     * @return string
62
     * @throws InvalidArgumentException
63
     */
64
    protected function getFilenameFor($latitude, $longitude)
65
    {
66
        $this->loadTileReferencesFor($latitude, $longitude);
67
68
        return sprintf($this->FilenameFormat, $this->CurrentTileHorizontalReference, $this->CurrentTileVerticalReference);
69
    }
70
71
    /**
72
     * @param float $latitude
73
     * @param float $longitude
74
     */
75
    protected function loadTileReferencesFor($latitude, $longitude)
76
    {
77
        if (fmod($latitude, static::DEGREES_PER_TILE) === 0.0) {
78
            $this->CurrentTileVerticalReference = (static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE + 1;
79
        } else {
80
            $this->CurrentTileVerticalReference = ceil((static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE);
81
        }
82
83
        if (fmod($longitude, static::DEGREES_PER_TILE) === 0.0) {
84
            $this->CurrentTileHorizontalReference = (static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE + 1;
85
        } else {
86
            $this->CurrentTileHorizontalReference = ceil((static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE);
87
        }
88
89
        $this->CurrentTileLatitude = static::MAX_LATITUDE - (($this->CurrentTileVerticalReference - 1) * static::DEGREES_PER_TILE);
90
        $this->CurrentTileLongitude = (($this->CurrentTileHorizontalReference - 1) * static::DEGREES_PER_TILE) - static::MAX_LONGITUDE;
91
92
        if ($latitude < 0) {
93
            $this->CurrentTileLatitude = -$this->CurrentTileLatitude;
0 ignored issues
show
Documentation Bug introduced by
The property $CurrentTileLatitude was declared of type double, but -$this->CurrentTileLatitude is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
94
        }
95
    }
96
97
    /**
98
     * @param  float   $latitude
99
     * @param  float   $longitude
100
     * @return float[] array(row, col)
101
     */
102 View Code Duplication
    protected function getExactRowAndColFor($latitude, $longitude)
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...
103
    {
104
        return $this->ResourceReader->getExactRowAndColFor(
105
            abs($this->CurrentTileLatitude - abs($latitude)) / static::DEGREES_PER_TILE,
106
            abs($this->CurrentTileLongitude - $longitude) / static::DEGREES_PER_TILE
107
        );
108
    }
109
}
110