Completed
Push — master ( 55b21a...aec22b )
by Thomas
06:20
created

CodeFileGenerator::configure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
namespace gossi\codegen\generator;
3
4
use gossi\codegen\config\CodeFileGeneratorConfig;
5
use gossi\codegen\model\GenerateableInterface;
6
use gossi\docblock\Docblock;
7
use phootwork\lang\Text;
8
9
/**
10
 * Code file generator.
11
 *
12
 * Generates code for a model and puts it into a file with `<?php` statements. Can also
13
 * generate header comments.
14
 *
15
 * @author Thomas Gossmann
16
 */
17
class CodeFileGenerator extends CodeGenerator {
18
19
	/**
20
	 * Creates a new CodeFileGenerator
21
	 *
22
	 * @see https://php-code-generator.readthedocs.org/en/latest/generator.html
23
	 * @param CodeFileGeneratorConfig|array $config
24
	 */
25 6
	public function __construct($config = null) {
26 6
		parent::__construct($config);
27 6
	}
28
	
29 6
	protected function configure($config = null) {
30 6
		if (is_array($config)) {
31 5
			$this->config = new CodeFileGeneratorConfig($config);
32 1
		} else if ($config instanceof CodeFileGeneratorConfig) {
33 1
			$this->config = $config;
34
		} else {
35 1
			$this->config = new CodeFileGeneratorConfig();
36
		}
37 6
	}
38
39
	/**
40
	 * {@inheritDoc}
41
	 *
42
	 * @return CodeFileGeneratorConfig
43
	 */
44 1
	public function getConfig() {
45 1
		return $this->config;
46
	}
47
48
	/**
49
	 * {@inheritDoc}
50
	 */
51 5
	public function generate(GenerateableInterface $model) {
52 5
		$content = "<?php\n";
53
54 5
		$comment = $this->config->getHeaderComment();
55 5
		if ($comment !== null && !$comment->isEmpty()) {
56 1
			$content .= str_replace('/**', '/*', $comment->toString()) . "\n";
57
		}
58
59 5
		$docblock = $this->config->getHeaderDocblock();
60 5
		if ($docblock !== null && !$docblock->isEmpty()) {
61 1
			$content .= $docblock->toString() . "\n";
62
		}
63
64 5
		if ($this->config->getDeclareStrictTypes()) {
65 1
			$content .= "declare(strict_types=1);\n\n";
66
		}
67
68 5
		$content .= parent::generate($model);
69
70 5
		if ($this->config->getBlankLineAtEnd() && !Text::create($content)->endsWith("\n")) {
1 ignored issue
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class gossi\codegen\config\CodeGeneratorConfig as the method getBlankLineAtEnd() does only exist in the following sub-classes of gossi\codegen\config\CodeGeneratorConfig: gossi\codegen\config\CodeFileGeneratorConfig. 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...
71 4
			$content .= "\n";
72
		}
73
74 5
		return $content;
75
	}
76
}
77