ClassExample::demonstrate()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 8.8509
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

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
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-05-24 
5
 */
6
7
require_once 'AbstractExample.php';
8
require_once __DIR__ . '/../vendor/autoload.php';
9
10
/**
11
 * Class ClassExample
12
 * @package Net\Bazzline\Component\CodeGenerator\Example
13
 */
14
class ClassExample extends AbstractExample
15
{
16
    /**
17
     * @return string
18
     */
19
    function demonstrate()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21
        $blockFactory           = $this->getBlockGeneratorFactory();
22
        $classFactory           = $this->getClassGeneratorFactory();
23
        $constantFactory        = $this->getConstantGeneratorFactory();
24
        $documentationFactory   = $this->getDocumentationGeneratorFactory();
25
        $methodFactory          = $this->getMethodGeneratorFactory();
26
        $propertyFactory        = $this->getPropertyGeneratorFactory();
27
        $traitFactory           = $this->getTraitGeneratorFactory();
28
29
        $myConstant = $constantFactory->create();
30
        $myConstant->setName('MY_CONSTANT');
31
        $myConstant->setValue('foobar');
32
33
        $myProperty = $propertyFactory->create();
34
        $myProperty->setDocumentation($documentationFactory->create());
35
        $myProperty->markAsProtected();
36
        $myProperty->setName('myProperty');
37
        $myProperty->setValue(12345678.90);
38
        $myProperty->addTypeHint('float');
39
40
        $myMethod = $methodFactory->create();
41
        $myMethod->setDocumentation($documentationFactory->create());
42
        $myMethod->markAsPublic();
43
        $myMethod->markAsFinal();
44
        $myMethod->setName('myMethod');
45
        $myMethod->addParameter('foo', null, 'Foo');
46
        $myMethod->addParameter('bar', 'null', 'Bar');
47
        $myMethodBody = $blockFactory->create();
48
        $myMethodBody
49
            ->add('$foobar = $foo->toString();')
50
            ->add('')
51
            ->add('if (!is_null($bar)) {')
52
            ->startIndention()
53
                ->add('$foobar .= $bar->toString();')
54
            ->stopIndention()
55
            ->add('}')
56
            ->add('')
57
            ->add('return $foobar');
58
        $myMethod->setBody($myMethodBody, 'string');
0 ignored issues
show
Documentation introduced by
'string' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
60
        $myTrait = $traitFactory->create();
61
        $myTrait->setDocumentation($documentationFactory->create());
62
        $myTrait->setName('myTrait');
63
64
        $myClass = $classFactory->create();
65
        $myClass->setDocumentation($documentationFactory->create());
66
        $myClass->setNamespace('My\Namespace');
67
        $myClass->setName('MyClass');
68
        $myClass->markAsFinal();
69
        $myClass->setExtends('Foo\Bar', true);
70
        $myClass->addImplements('Bar\FooInterface', true);
71
        $myClass->addConstant($myConstant);
72
        $myClass->addMethod($myMethod);
73
        $myClass->addProperty($myProperty);
74
        $myClass->addTrait($myTrait);
75
        $myClass->getDocumentation()->setAuthor('stev leibelt', '[email protected]');
0 ignored issues
show
Bug introduced by
The method setAuthor cannot be called on $myClass->getDocumentation() (of type null|string|array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
76
        $myClass->getDocumentation()->setVersion('0.8.15', 'available since 2014-05-24');
0 ignored issues
show
Bug introduced by
The method setVersion cannot be called on $myClass->getDocumentation() (of type null|string|array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
77
78
        echo $myClass->generate() . PHP_EOL;
79
    }
80
}
81
82
$example = new ClassExample();
83
$example->demonstrate();