Completed
Pull Request — 3.4 (#46)
by David
22:22 queued 03:02
created

ScalarBeanPropertyDescriptor::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
4
namespace Mouf\Database\TDBM\Utils;
5
6
use Doctrine\DBAL\Schema\Column;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
9
10
/**
11
 * This class represent a property in a bean (a property has a getter, a setter, etc...)
12
 */
13
class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor
14
{
15
    /**
16
     * @var Column
17
     */
18
    private $column;
19
20
21
    public function __construct(Table $table, Column $column) {
22
        parent::__construct($table);
23
        $this->table = $table;
24
        $this->column = $column;
25
    }
26
27
    /**
28
     * Returns the foreignkey the column is part of, if any. null otherwise.
29
     *
30
     * @param Column $column
0 ignored issues
show
Bug introduced by
There is no parameter named $column. 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...
31
     * @return ForeignKeyConstraint|null
32
     */
33
    public function getForeignKey() {
34
        return false;
35
    }
36
37
    /**
38
     * Returns the param annotation for this property (useful for constructor).
39
     *
40
     * @return string
41
     */
42
    public function getParamAnnotation() {
43
        $className = $this->getClassName();
44
        $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType());
45
46
        $str = "     * @param %s %s";
47
        return sprintf($str, $paramType, $this->getVariableName());
48
    }
49
50
    public function getUpperCamelCaseName() {
51
        return TDBMDaoGenerator::toCamelCase($this->column->getName());
52
    }
53
54
    /**
55
     * Returns the name of the class linked to this property or null if this is not a foreign key
56
     * @return null|string
57
     */
58
    public function getClassName() {
59
        return null;
60
    }
61
62
    /**
63
     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
64
     * @return bool
65
     */
66
    public function isCompulsory() {
67
        return $this->column->getNotnull() && !$this->column->getAutoincrement();
68
    }
69
70
    /**
71
     * Returns true if the property is the primary key
72
     * @return bool
73
     */
74
    public function isPrimaryKey() {
75
        return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns());
76
    }
77
78
    /**
79
     * Returns the PHP code for getters and setters
80
     * @return string
81
     */
82
    public function getGetterSetterCode() {
83
84
        $type = $this->column->getType();
85
        $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
86
87
        $columnGetterName = $this->getGetterName();
88
        $columnSetterName = $this->getSetterName();
89
90
        if ($normalizedType == "\\DateTimeInterface") {
91
            $castTo = "\\DateTimeInterface ";
92
        } else {
93
            $castTo = "";
94
        }
95
96
        $getterAndSetterCode = '    /**
97
     * The getter for the "%s" column.
98
     *
99
     * @return %s
100
     */
101
    public function %s() {
102
        return $this->get(%s, %s);
103
    }
104
105
    /**
106
     * The setter for the "%s" column.
107
     *
108
     * @param %s $%s
109
     */
110
    public function %s(%s$%s) {
111
        $this->set(%s, $%s, %s);
112
    }
113
114
';
115
        return sprintf($getterAndSetterCode,
116
            // Getter
117
            $this->column->getName(),
118
            $normalizedType,
119
            $columnGetterName,
120
            var_export($this->column->getName(), true),
121
            var_export($this->table->getName(), true),
122
            // Setter
123
            $this->column->getName(),
124
            $normalizedType,
125
            $this->column->getName(),
126
            $columnSetterName,
127
            $castTo,
128
            $this->column->getName(),
129
            var_export($this->column->getName(), true),
130
            $this->column->getName(),
131
            var_export($this->table->getName(), true)
132
        );
133
    }
134
135
    /**
136
     * Returns the part of code useful when doing json serialization.
137
     *
138
     * @return string
139
     */
140
    public function getJsonSerializeCode()
141
    {
142
        $type = $this->column->getType();
143
        $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type);
144
145
        if ($normalizedType == "\\DateTimeInterface") {
146
            return '        $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."()->format('c');\n";
147
        } else {
148
            return '        $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n";
149
        }
150
    }
151
}
152