Completed
Push — master ( c9073d...17137d )
by Thomas
17:02
created

AbstractResponseGenerator::ensureUseStatements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace keeko\tools\generator;
3
4
use gossi\codegen\model\AbstractPhpStruct;
5
use gossi\codegen\model\PhpMethod;
6
use gossi\codegen\model\PhpParameter;
7
use keeko\tools\generator\AbstractCodeGenerator;
8
use keeko\core\schema\ActionSchema;
9
use gossi\codegen\model\PhpClass;
10
11
abstract class AbstractResponseGenerator extends AbstractCodeGenerator {
12
	
13 2
	protected function getTemplateFolder() {
14 2
		return 'response';
15
	}
16
	
17
	/**
18
	 * Generates a response class for the given action
19
	 *
20
	 * @param ActionSchema $action
21
	 * @return PhpClass
22
	 */
23
	abstract public function generate(ActionSchema $action);
24
	
25
	/**
26
	 * Generates a response class for the given action
27
	 *
28
	 * @param ActionSchema $action
29
	 * @param string $format
30
	 * @return AbstractPhpStruct
31
	 */
32 5
	protected function doGenerate(ActionSchema $action, $format) {
33 5
		$struct = $this->generateStruct($action, $format);
34
	
35 5
		$this->codegenService->addAuthors($struct, $this->packageService->getPackage());
36
	
37 5
		$this->ensureUseStatements($struct);
38 5
		$this->addMethods($struct, $action);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class keeko\tools\generator\AbstractResponseGenerator as the method addMethods() does only exist in the following sub-classes of keeko\tools\generator\AbstractResponseGenerator: keeko\tools\generator\re...nkHtmlResponseGenerator, keeko\tools\generator\re...nkJsonResponseGenerator, keeko\tools\generator\re...teJsonResponseGenerator, keeko\tools\generator\re...teJsonResponseGenerator, keeko\tools\generator\re...mpJsonResponseGenerator, keeko\tools\generator\re...stJsonResponseGenerator, keeko\tools\generator\re...adJsonResponseGenerator, keeko\tools\generator\re...igHtmlResponseGenerator, keeko\tools\generator\re...teJsonResponseGenerator, keeko\tools\generator\re...lResponseTraitGenerator. 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...
39
	
40 5
		return $struct;
41
	}
42
	
43
	/**
44
	 * Generates the struct
45
	 *
46
	 * @param ActionSchema $action
47
	 * @param string $format
48
	 * @return AbstractPhpStruct
49
	 */
50 5
	protected function generateStruct(ActionSchema $action, $format) {
51 5
		return PhpClass::create($action->getResponse($format))
52 5
			->setParentClassName('AbstractResponse')
53 5
			->setDescription('Automatically generated ' . ucwords($format) . 'Response for ' . $action->getTitle())
54 5
			->setLongDescription($action->getDescription());
55
	}
56
	
57 5
	protected function generateRunMethod($body = '') {
58 5
		return PhpMethod::create('run')
59 5
			->setDescription('Automatically generated run method')
60 5
			->setType('Response')
61 5
			->addParameter(PhpParameter::create('request')->setType('Request'))
62 5
			->addParameter(PhpParameter::create('data')->setDefaultValue(null))
63 5
			->setBody($body);
64
	}
65
66 5
	protected function ensureUseStatements(AbstractPhpStruct $struct) {
67 5
		$struct->addUseStatement('keeko\\core\\package\\AbstractResponse');
68 5
		$struct->addUseStatement('Symfony\\Component\\HttpFoundation\\Request');
69 5
		$struct->addUseStatement('Symfony\\Component\\HttpFoundation\\Response');
70 5
	}
71
72
}