Completed
Push — master ( 98cd2e...56265b )
by Thomas
09:01
created

AbstractResponseGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 8
dl 0
loc 62
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getTemplateFolder() 0 3 1
generate() 0 1 ?
A doGenerate() 0 10 1
A generateStruct() 0 6 1
A generateRunMethod() 0 8 1
A ensureUseStatements() 0 5 1
1
<?php
2
namespace keeko\tools\generator\response;
3
4
use gossi\codegen\model\AbstractPhpStruct;
5
use gossi\codegen\model\PhpClass;
6
use gossi\codegen\model\PhpMethod;
7
use gossi\codegen\model\PhpParameter;
8
use keeko\framework\schema\ActionSchema;
9
use keeko\tools\generator\AbstractCodeGenerator;
10
11
abstract class AbstractResponseGenerator extends AbstractCodeGenerator {
12
	
13
	protected function getTemplateFolder() {
14
		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
	protected function doGenerate(ActionSchema $action, $format) {
33
		$struct = $this->generateStruct($action, $format);
34
	
35
		$this->codegenService->addAuthors($struct, $this->packageService->getPackage());
36
	
37
		$this->ensureUseStatements($struct);
38
		$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\re...stractResponseGenerator as the method addMethods() does only exist in the following sub-classes of keeko\tools\generator\re...stractResponseGenerator: keeko\tools\generator\re...nkJsonResponseGenerator, keeko\tools\generator\re...mpJsonResponseGenerator, keeko\tools\generator\re...teJsonResponseGenerator, keeko\tools\generator\re...teJsonResponseGenerator, keeko\tools\generator\re...stJsonResponseGenerator, keeko\tools\generator\re...adJsonResponseGenerator, keeko\tools\generator\re...teJsonResponseGenerator, keeko\tools\generator\re...ipJsonResponseGenerator, keeko\tools\generator\re...ipJsonResponseGenerator, 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
		return $struct;
41
	}
42
	
43
	/**
44
	 * Generates the struct
45
	 *
46
	 * @param ActionSchema $action
47
	 * @param string $format
48
	 * @return AbstractPhpStruct
49
	 */
50
	protected function generateStruct(ActionSchema $action, $format) {
51
		return PhpClass::create($action->getResponse($format))
52
			->setParentClassName('AbstractResponse')
53
			->setDescription('Automatically generated ' . ucwords($format) . 'Response for ' . $action->getTitle())
54
			->setLongDescription($action->getDescription());
55
	}
56
	
57
	protected function generateRunMethod($body = '') {
58
		return PhpMethod::create('run')
59
			->setDescription('Automatically generated run method')
60
			->setType('Response')
61
			->addParameter(PhpParameter::create('request')->setType('Request'))
62
			->addParameter(PhpParameter::create('data')->setDefaultValue(null))
63
			->setBody($body);
64
	}
65
66
	protected function ensureUseStatements(AbstractPhpStruct $struct) {
67
		$struct->addUseStatement('keeko\\framework\\foundation\\AbstractResponse');
68
		$struct->addUseStatement('Symfony\\Component\\HttpFoundation\\Request');
69
		$struct->addUseStatement('Symfony\\Component\\HttpFoundation\\Response');
70
	}
71
72
}