AbstractModel::export()   C
last analyzed

Complexity

Conditions 13
Paths 36

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 28
cts 28
cp 1
rs 6.6166
c 0
b 0
f 0
cc 13
nc 36
nop 0
crap 13

How to fix   Complexity   

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
namespace gossi\swagger;
3
4
use phootwork\collection\Collection;
5
use phootwork\collection\CollectionUtils;
6
7
abstract class AbstractModel {
8
9 11
	protected function export() {
10 11
		$cols = func_get_args();
11
12
		// add cols
13 11
		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...
14 6
			$cols = array_merge(['$ref'], $cols);
15 6
		}
16
17
		// flatten array
18 11
		$fields = [];
19
		array_walk_recursive($cols, function ($a) use (&$fields) { $fields[] = $a; });
20
21 11
		$out = [];
22 11
		$refl = new \ReflectionClass(get_class($this));
23
24 11
		foreach ($fields as $field) {
25 11
			if ($field == 'tags') {
26 8
				$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...
27 8
			} else {
28 11
				$prop = $refl->getProperty($field == '$ref' ? 'ref' : $field);
29 11
				$prop->setAccessible(true);
30 11
				$val = $prop->getValue($this);
31
32 11
				if ($val instanceof Collection) {
33 11
					$val = CollectionUtils::toArrayRecursive($val);
34 11
				} else if (is_object($val) && method_exists($val, 'toArray')) {
35 11
					$val = $val->toArray();
36 11
				}
37
			}
38
39 11
			if ($field == 'required' && is_bool($val) || !empty($val)) {
40 9
				$out[$field] = $val;
41 9
			}
42 11
		}
43
44 11
		if (method_exists($this, 'getExtensions')) {
45 11
			$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...
46 11
		}
47
48 11
		return $out;
49
	}
50
51
}
52