Completed
Push — master ( f12f85...ac41f4 )
by Hannes
02:45
created

SRTM4Provider::initResourceReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
class SRTM4Provider extends AbstractGeoTIFFProvider
15
{
16
    /** @var int */
17
    const MAX_LATITUDE = 60;
18
19
    /** @var int */
20
    const MAX_LONGITUDE = 180;
21
22
    /** @var float */
23
    const DEGREES_PER_TILE = 5.0;
24
25
    /** @var float */
26
    protected $CurrentTileHorizontalReference;
27
28
    /** @var float */
29
    protected $CurrentTileVerticalReference;
30
31
    /** @var float top left latitude */
32
    protected $CurrentTileLatitude;
33
34
    /** @var float top left longitude */
35
    protected $CurrentTileLongitude;
36
37
    /** @var string */
38
    protected $FilenameFormat = 'srtm_%02d_%02d.tif';
39
40 8
    public function initResourceReader()
41
    {
42 8
        $this->ResourceReader = new GeoTIFFReader();
43 8
    }
44
45
    /**
46
     * @param $format
47
     */
48
    public function setFilenameFormat($format)
49
    {
50
        $this->FilenameFormat = $format;
51
    }
52
53
    /**
54
     * @param  float                     $latitude
55
     * @param  float                     $longitude
56
     * @return string
57
     * @throws \InvalidArgumentException
58
     */
59 7
    protected function getFilenameFor($latitude, $longitude)
60
    {
61 7
        $this->loadTileReferencesFor($latitude, $longitude);
62
63 7
        return sprintf($this->FilenameFormat, $this->CurrentTileHorizontalReference, $this->CurrentTileVerticalReference);
64
    }
65
66
    /**
67
     * @param float $latitude
68
     * @param float $longitude
69
     */
70 7
    protected function loadTileReferencesFor($latitude, $longitude)
71
    {
72 7
        if (fmod($latitude, static::DEGREES_PER_TILE) === 0) {
73
            $this->CurrentTileVerticalReference = (static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE + 1;
74
        } else {
75 7
            $this->CurrentTileVerticalReference = ceil((static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE);
76
        }
77
78 7
        if (fmod($longitude, static::DEGREES_PER_TILE) === 0) {
79
            $this->CurrentTileHorizontalReference = (static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE + 1;
80
        } else {
81 7
            $this->CurrentTileHorizontalReference = ceil((static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE);
82
        }
83
84 7
        $this->CurrentTileLatitude = static::MAX_LATITUDE - (($this->CurrentTileVerticalReference - 1) * static::DEGREES_PER_TILE);
85 7
        $this->CurrentTileLongitude = (($this->CurrentTileHorizontalReference - 1) * static::DEGREES_PER_TILE) - static::MAX_LONGITUDE;
86
87 7
        if ($latitude < 0) {
88 3
            $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...
89 3
        }
90 7
    }
91
92
    /**
93
     * @param  float   $latitude
94
     * @param  float   $longitude
95
     * @return float[] array(row, col)
96
     */
97 6
    protected function getExactRowAndColFor($latitude, $longitude)
98
    {
99 6
        return $this->ResourceReader->getExactRowAndColFor(
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Runalyze\DEM\Provider\ResourceReaderInterface as the method getExactRowAndColFor() does only exist in the following implementations of said interface: Runalyze\DEM\Provider\GeoTIFF\GeoTIFFReader.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
100 6
            abs($this->CurrentTileLatitude - abs($latitude)) / static::DEGREES_PER_TILE,
101 6
            abs($this->CurrentTileLongitude - $longitude) / static::DEGREES_PER_TILE
102 6
        );
103
    }
104
}
105