Completed
Push — master ( 8619b3...819e89 )
by Thomas
05:42
created

src/AbstractModel.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace gossi\swagger;
3
4
abstract class AbstractModel {
5
	
6 6
	protected function export() {
7 6
		$cols = func_get_args();
8
		
9
		// add cols
10 6
		if (method_exists($this, 'hasRef') && $this->hasRef()) {
0 ignored issues
show
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. 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...
11 5
			$cols = array_merge(['$ref'], $cols);
12 5
		}
13
		
14
		// flatten array
15 6
		$fields = [];
16
		array_walk_recursive($cols, function($a) use (&$fields) { $fields[] = $a; });
0 ignored issues
show
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...
17
18 6
		$out = [];
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
19 6
		$refl = new \ReflectionClass(get_class($this));
20
		
21 6
		foreach ($fields as $field) {
22 6
			if ($field == 'tags') {
23 6
				$val = $this->exportTags();
0 ignored issues
show
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...
24 6
			} else {
25 6
				$prop = $refl->getProperty($field == '$ref' ? 'ref' : $field);
26 6
				$prop->setAccessible(true);
27 6
				$val = $prop->getValue($this);
28
				
29 6
				if (method_exists($val, 'toArray')) {
30 6
					$val = $val->toArray();
31 6
				}
32
			}
33
			
34 6
			if ($field == 'required' && is_bool($val) || !empty($val)) {
35 6
				$out[$field] = $val;
36 6
			}
37 6
		}
38
		
39 6
		if (method_exists($this, 'getExtensions')) {
40 6
			$out = array_merge($out, $this->getExtensions()->toArray());
41 6
		}
42
		
43 6
		return $out;
44
	}
45
}