Completed
Pull Request — master (#6)
by Guilh
02:50
created

AbstractModel::mergeFields()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.4425

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 10
cts 13
cp 0.7692
rs 8.8571
cc 6
eloc 10
nc 5
nop 3
crap 6.4425
1
<?php
2
namespace gossi\swagger;
3
4
use phootwork\collection\Collection;
5
use phootwork\collection\CollectionUtils;
6
7
abstract class AbstractModel {
8
	/**
9
	 * Prefers the original model when merging two models.
10
	 */
11
	const PREFER_ORIGINAL = 0;
12
	/**
13
	 * Prefers the external model when merging two models.
14
	 */
15
	const PREFER_EXTERNAL = 1;
16
17 10
	protected function export() {
18 10
		$cols = func_get_args();
19
20
		// add cols
21 10
		if (method_exists($this, 'hasRef') && $this->hasRef()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class gossi\swagger\AbstractModel as the method hasRef() does only exist in the following sub-classes of gossi\swagger\AbstractModel: gossi\swagger\Items, gossi\swagger\Parameter, gossi\swagger\Response, gossi\swagger\Schema, gossi\swagger\collections\Parameters. 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...
22 6
			$cols = array_merge(['$ref'], $cols);
23 6
		}
24
25
		// flatten array
26 10
		$fields = [];
27
		array_walk_recursive($cols, function ($a) use (&$fields) { $fields[] = $a; });
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
28
29 10
		$out = [];
30 10
		$refl = new \ReflectionClass(get_class($this));
31
32 10
		foreach ($fields as $field) {
33 10
			if ($field == 'tags') {
34 7
				$val = $this->exportTags();
0 ignored issues
show
Bug introduced by
The method exportTags() does not exist on gossi\swagger\AbstractModel. Did you maybe mean export()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
35 7
			} else {
36 10
				$prop = $refl->getProperty($field == '$ref' ? 'ref' : $field);
37 10
				$prop->setAccessible(true);
38 10
				$val = $prop->getValue($this);
39
40 10
				if ($val instanceof Collection) {
41 10
					$val = CollectionUtils::toArrayRecursive($val);
42 10
				} else if (method_exists($val, 'toArray')) {
43 10
					$val = $val->toArray();
44 10
				}
45
			}
46
47 10
			if ($field == 'required' && is_bool($val) || !empty($val)) {
48 8
				$out[$field] = $val;
49 8
			}
50 10
		}
51
52 10
		if (method_exists($this, 'getExtensions')) {
53 10
			$out = array_merge($out, $this->getExtensions()->toArray());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class gossi\swagger\AbstractModel as the method getExtensions() does only exist in the following sub-classes of gossi\swagger\AbstractModel: gossi\swagger\Contact, gossi\swagger\ExternalDocs, gossi\swagger\Header, gossi\swagger\Info, gossi\swagger\Items, gossi\swagger\License, gossi\swagger\Operation, gossi\swagger\Parameter, gossi\swagger\Path, gossi\swagger\Response, gossi\swagger\Schema, gossi\swagger\Swagger, gossi\swagger\Tag, gossi\swagger\collections\Paths, gossi\swagger\collections\Responses. 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...
54 10
		}
55
56 10
		return $out;
57
	}
58
59 10
	protected function mergeFields(&$original, $external, $strategy)
60
	{
61 10
		if ($original instanceof self && method_exists($original, 'merge')) {
62 10
			$original->merge($external, $strategy);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class gossi\swagger\AbstractModel as the method merge() does only exist in the following sub-classes of gossi\swagger\AbstractModel: gossi\swagger\Contact, gossi\swagger\ExternalDocs, gossi\swagger\Header, gossi\swagger\Info, gossi\swagger\License. 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...
63
64 10
			return;
65
		}
66
67 10
		if (self::PREFER_ORIGINAL === $strategy) {
68 10
			if (null === $original) {
69 10
				$original = $external;
70 10
			}
71 10
		} else {
72
			if (null !== $external) {
73
				$original = $external;
74
			}
75
		}
76 10
	}
77
}
78