Completed
Push — main ( 8c4dae...cd35aa )
by Christian
02:46 queued 01:22
created

MyTestCase   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 23
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertInternalType() 0 20 5
1
<?php
2
3
/*
4
 * OpenWeatherMap-PHP-API — A PHP API to parse weather data from https://OpenWeatherMap.org.
5
 *
6
 * @license MIT
7
 *
8
 * Please see the LICENSE file distributed with this source code for further
9
 * information regarding copyright and licensing.
10
 *
11
 * Please visit the following links to read about the usage policies and the license of
12
 * OpenWeatherMap data before using this library:
13
 *
14
 * @see https://OpenWeatherMap.org/price
15
 * @see https://OpenWeatherMap.org/terms
16
 * @see https://OpenWeatherMap.org/appid
17
 */
18
19
namespace Cmfcmf\OpenWeatherMap\Tests;
20
21
abstract class MyTestCase extends \PHPUnit\Framework\TestCase
22
{
23
    public static function assertInternalType(string $expected, $actual, string $message = ''): void
24
    {
25
        if (version_compare(phpversion(), '7.2', '>=')) {
26
            switch ($expected) {
27
            case 'string':
28
                static::assertIsString($actual);
29
                break;
30
            case 'object':
31
                static::assertIsObject($actual);
32
                break;
33
            case 'float':
34
                static::assertIsFloat($actual);
35
                break;
36
            default:
37
                throw new Error();
38
            }
39
        } else {
40
            \PHPUnit\Framework\TestCase::assertInternalType($expected, $actual, $message);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PHPUnit\Framework\TestCase as the method assertInternalType() does only exist in the following sub-classes of PHPUnit\Framework\TestCase: Cmfcmf\OpenWeatherMap\Tests\MyTestCase, Cmfcmf\OpenWeatherMap\Tests\OpenWeatherMapTest, Cmfcmf\OpenWeatherMap\Te...CurrentWeatherGroupTest, Cmfcmf\OpenWeatherMap\Te...Map\WeatherForecastTest, Cmfcmf\OpenWeatherMap\Tests\Util\TemperatureTest. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
41
        }
42
    }
43
}
44