GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch master (c6c701)
by Petko
30:18 queued 26:36
created

PetkoparaFormGenerator::generate()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 36
Code Lines 24

Duplication

Lines 3
Ratio 8.33 %

Code Coverage

Tests 24
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 3
loc 36
ccs 24
cts 26
cp 0.9231
rs 8.439
c 0
b 0
f 0
cc 5
eloc 24
nc 3
nop 4
crap 5.0113
1
<?php namespace Petkopara\CrudGeneratorBundle\Generator;
2
3
use Doctrine\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory;
4
use Doctrine\ORM\Mapping\ClassMetadataInfo;
5
use Petkopara\CrudGeneratorBundle\Generator\Guesser\MetadataGuesser;
6
use RuntimeException;
7
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator;
8
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
9
10
/**
11
 * Generates a form class based on a Doctrine entity.
12
 *
13
 */
14
class PetkoparaFormGenerator extends DoctrineFormGenerator
15
{
16
17
    private $className;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
    private $classPath;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
19
    private $metadataGuesser;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param DisconnectedMetadataFactory $metadataFactory DisconnectedMetadataFactory instance
0 ignored issues
show
Bug introduced by
There is no parameter named $metadataFactory. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     */
26 1
    public function __construct(MetadataGuesser $guesser)
27
    {
28 1
        $this->metadataGuesser = $guesser;
29 1
    }
30
31
    public function getClassName()
32
    {
33
        return $this->className;
34
    }
35
36
    public function getClassPath()
37
    {
38
        return $this->classPath;
39
    }
40
41
    /**
42
     * Generates the entity form class.
43
     *
44
     * @param BundleInterface   $bundle         The bundle in which to create the class
45
     * @param string            $entity         The entity relative class name
46
     * @param ClassMetadataInfo $metadata       The entity metadata class
47
     * @param bool              $forceOverwrite If true, remove any existing form class before generating it again
48
     */
49 1
    public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $forceOverwrite = false)
50
    {
51 1
        $parts = explode('\\', $entity);
52 1
        $entityClass = array_pop($parts);
53
54 1
        $this->className = $entityClass . 'Type';
55 1
        $dirPath = $bundle->getPath() . '/Form';
56 1
        $this->classPath = $dirPath . '/' . str_replace('\\', '/', $entity) . 'Type.php';
57
58 1 View Code Duplication
        if (!$forceOverwrite && file_exists($this->classPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            throw new RuntimeException(sprintf('Unable to generate the %s form class as it already exists under the %s file', $this->className, $this->classPath));
60
        }
61
62 1
        if (count($metadata->identifier) > 1) {
63
            throw new RuntimeException('The form generator does not support entity classes with multiple primary keys.');
64
        }
65
66 1
        $parts = explode('\\', $entity);
67 1
        array_pop($parts);
68
69 1
        $this->renderFile('form/FormType.php.twig', $this->classPath, array(
70 1
            'fields' => $this->getFieldsFromMetadata($metadata),
71 1
            'fields_associated' => $this->getAssociatedFields($metadata),
72 1
            'fields_mapping' => $metadata->fieldMappings,
73 1
            'namespace' => $bundle->getNamespace(),
74 1
            'entity_namespace' => implode('\\', $parts),
75 1
            'entity_class' => $entityClass,
76 1
            'bundle' => $bundle->getName(),
77 1
            'form_class' => $this->className,
78 1
            'form_type_name' => strtolower(str_replace('\\', '_', $bundle->getNamespace()) . ($parts ? '_' : '') . implode('_', $parts) . '_' . substr($this->className, 0, -4)),
79
            // Add 'setDefaultOptions' method with deprecated type hint, if the new 'configureOptions' isn't available.
80
            // Required as long as Symfony 2.6 is supported.
81 1
            'configure_options_available' => method_exists('Symfony\Component\Form\AbstractType', 'configureOptions'),
82 1
            'get_name_required' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
83 1
        ));
84 1
    }
85
86
    /**
87
     * Returns an array of fields. Fields can be both column fields and
88
     * association fields.
89
     *
90
     * @param ClassMetadataInfo $metadata
91
     *
92
     * @return array $fields
93
     */
94 1
    private function getFieldsFromMetadata(ClassMetadataInfo $metadata)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
95
    {
96 1
        $fields = (array) $metadata->fieldNames;
97
98
        // Remove the primary key field if it's not managed manually
99 1
        if (!$metadata->isIdentifierNatural()) {
100 1
            $fields = array_diff($fields, $metadata->identifier);
101 1
        }
102
103 1
        return $fields;
104
    }
105
106 1
    private function getAssociatedFields(ClassMetadataInfo $metadata)
107
    {
108 1
        $fields = array();
109
110 1
        foreach ($metadata->associationMappings as $fieldName => $relation) {
111
            
112 1
            switch ($relation['type']) {
113 1
                case ClassMetadataInfo::ONE_TO_ONE:
114 1
                case ClassMetadataInfo::MANY_TO_ONE:
115 1
                    $fields[$fieldName] = $this->getRelationFieldData($fieldName, $relation, "MANY_TO_ONE");
116 1
                    break;
117 1
                case ClassMetadataInfo::MANY_TO_MANY:
118
                    $fields[$fieldName] = $this->getRelationFieldData($fieldName, $relation, "MANY_TO_MANY");
119
                    break;
120 1
                case ClassMetadataInfo::ONE_TO_MANY:
121
                    $fields[$fieldName] = $this->getRelationFieldData($fieldName, $relation, "ONE_TO_MANY");
122
                    break;
123 1
            }
124 1
        }
125
126 1
        return $fields;
127
    }
128
129
    /**
130
     * @param string $relationType
131
     */
132 1
    private function getRelationFieldData($fieldName, $relation, $relationType)
133
    {
134 1
        $field['name'] = $fieldName;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$field was never initialized. Although not strictly required by PHP, it is generally a good practice to add $field = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
135 1
        $field['widget'] = 'EntityType::class';
136 1
        $field['class'] = $relation['targetEntity'];
137 1
        $field['choice_label'] = $this->metadataGuesser->guessChoiceLabelFromClass($relation['targetEntity']);
138 1
        $field['type'] = $relationType;
139 1
        return $field;
140
    }
141
142
 
143
}
144