sunnysideup /
silverstripe-dataobject-generator
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SunnySideUp\BuildDataObject; |
||
| 4 | |||
| 5 | class API extends \Object |
||
| 6 | { |
||
| 7 | private static $excluded_data_objects = [ |
||
|
0 ignored issues
–
show
|
|||
| 8 | 'Image_Cached', |
||
| 9 | 'PermissionRoleCode', |
||
| 10 | 'LoginAttempt', |
||
| 11 | 'MemberPassword', |
||
| 12 | 'MemberPassword', |
||
| 13 | 'SiteConfig' |
||
| 14 | ]; |
||
| 15 | |||
| 16 | private static $excluded_db_fields_types = [ |
||
|
0 ignored issues
–
show
|
|||
| 17 | 'DBField', |
||
| 18 | 'Field', |
||
| 19 | 'DBLocale', |
||
| 20 | 'Locale', |
||
| 21 | 'StringField', |
||
| 22 | 'CompositeField', |
||
| 23 | 'PrimaryKey', |
||
| 24 | 'ForeignKey' |
||
| 25 | ]; |
||
| 26 | |||
| 27 | private static $additional_db_fields = [ |
||
|
0 ignored issues
–
show
|
|||
| 28 | 'ID', |
||
| 29 | 'Created', |
||
| 30 | 'LastEdited', |
||
| 31 | 'ClassName' |
||
| 32 | ]; |
||
| 33 | |||
| 34 | protected $rootBaseClass = 'DataObject'; |
||
| 35 | |||
| 36 | protected $myBaseClass = ''; |
||
| 37 | |||
| 38 | protected $_data = null; |
||
| 39 | |||
| 40 | private static $_my_singleton = []; |
||
| 41 | |||
| 42 | public static function inst($myBaseClass = 'DataObject', $data) |
||
|
0 ignored issues
–
show
Parameters which have default values should be placed at the end.
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: // $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a Loading history...
|
|||
| 43 | { |
||
| 44 | if (! isset(self::$_my_singleton[$myBaseClass])) { |
||
| 45 | self::$_my_singleton[$myBaseClass] = \Injector::inst()->get('SunnySideUp\BuildDataObject\API'); |
||
| 46 | } |
||
| 47 | self::$_my_singleton[$myBaseClass]->_data = $data; |
||
| 48 | self::$_my_singleton[$myBaseClass]->setBaseClass($myBaseClass); |
||
| 49 | |||
| 50 | return self::$_my_singleton[$myBaseClass]; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function setBaseClass($myBaseClass) |
||
| 54 | { |
||
| 55 | $this->myBaseClass = $myBaseClass; |
||
| 56 | } |
||
| 57 | |||
| 58 | |||
| 59 | protected function retrieveDBFields($name) |
||
| 60 | { |
||
| 61 | $data = $this->_data; |
||
| 62 | $ar = []; |
||
| 63 | if (isset($data->$name)) { |
||
| 64 | foreach ($data->$name as $data) { |
||
| 65 | if ($data->Key && $data->Value) { |
||
| 66 | $ar[$data->Key] = $data->Key; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return $ar; |
||
| 72 | } |
||
| 73 | |||
| 74 | protected $_dbfieldCache = []; |
||
| 75 | |||
| 76 | public function DbFields() |
||
| 77 | { |
||
| 78 | if (count($this->_dbfieldCache) === 0) { |
||
| 79 | $list = \ClassInfo::subclassesFor('DbField'); |
||
| 80 | $newList = []; |
||
| 81 | foreach ($list as $class) { |
||
|
0 ignored issues
–
show
The expression
$list of type null|array is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 82 | if (substr($class, 0, 2) == 'DB') { |
||
| 83 | $class = substr($class, 2, strlen($class)); |
||
| 84 | } elseif (substr($class, 0, 3) == 'SS_') { |
||
| 85 | $class = substr($class, 3, strlen($class)); |
||
| 86 | } elseif ('Varchar' === $class) { |
||
| 87 | $class = 'Varchar'; |
||
| 88 | } elseif ('HTMLVarchar' === $class) { |
||
| 89 | $class = 'HTMLVarchar(255)'; |
||
| 90 | } elseif ('Enum' === $class) { |
||
| 91 | $class = 'Enum(\\\'Foo,Bar\\\', \\\'FOO\\\')'; |
||
| 92 | } elseif ('MultiEnum' === $class) { |
||
| 93 | $class = 'MultiEnum(\\\'Foo,Bar\\\', \\\'FOO\\\')'; |
||
| 94 | } |
||
| 95 | if ( |
||
|
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. Loading history...
|
|||
| 96 | $class == 'DbField' || |
||
| 97 | is_subclass_of($class, 'TestOnly') || |
||
| 98 | in_array($class, $this->Config()->get('excluded_db_fields_types')) |
||
| 99 | ) { |
||
| 100 | //do nothing |
||
| 101 | } else { |
||
| 102 | $newList[$class] = $class; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | $this->_dbfieldCache = $newList; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this->_dbfieldCache; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function MyDbFields() |
||
| 112 | { |
||
| 113 | return $this->retrieveDBFields('db'); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function MyDbFieldsWithDefaults() |
||
| 117 | { |
||
| 118 | $list = $this->retrieveDBFields('db'); |
||
| 119 | $toAdd = $this->Config()->get('additional_db_fields'); |
||
| 120 | $toAdd = array_combine($toAdd, $toAdd); |
||
| 121 | |||
| 122 | return $toAdd + $list; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function mydbfieldsfancywithbelongswithbasicfields() |
||
| 126 | { |
||
| 127 | return $this->MyDbFieldsFancyWithBelongs(); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function MyDbFieldsFancyWithBelongs() |
||
| 131 | { |
||
| 132 | return $this->myDbFieldsFancyWithoutBelongs(true); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function MyDbFieldsFancyWithoutBelongs($includeBelongs = false) |
||
| 136 | { |
||
| 137 | $ar = []; |
||
| 138 | $list = $this->retrieveDBFields('db'); |
||
| 139 | foreach ($list as $key => $value) { |
||
| 140 | $ar[$key] = $key; |
||
| 141 | $shortValue = explode('(', $value); |
||
| 142 | $shortValue = $shortValue[0]; |
||
| 143 | switch ($shortValue) { |
||
| 144 | case 'Varchar': |
||
| 145 | case 'HTMLTextField': |
||
| 146 | case 'HTMLVarchar': |
||
| 147 | case 'Text': |
||
| 148 | $ar[$key.'.LimitCharacters'] = $key.'.LimitCharacters'; |
||
| 149 | break; |
||
| 150 | default: |
||
| 151 | $ar[$key.'.Nice'] = $key.'.Nice'; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | $list = []; |
||
| 155 | if ($includeBelongs) { |
||
| 156 | $list += $this->retrieveDBFields('belongs_to'); |
||
| 157 | } |
||
| 158 | $list += $this->retrieveDBFields('has_one'); |
||
| 159 | foreach ($list as $key => $value) { |
||
| 160 | if ($value === 'Image' || is_subclass_of($value, 'Image')) { |
||
| 161 | $ar[$key.'.CMSThumbnail'] = $key.'.CMSThumbnail'; |
||
| 162 | } else { |
||
| 163 | $ar[$key.'.Title'] = $key.'.Title'; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | $list = |
||
| 167 | $this->retrieveDBFields('has_many') + |
||
| 168 | $this->retrieveDBFields('many_many'); |
||
| 169 | if ($includeBelongs) { |
||
| 170 | $list += $this->retrieveDBFields('belongs_many_many'); |
||
| 171 | } |
||
| 172 | foreach ($list as $key => $value) { |
||
| 173 | $ar[$key.'.Count'] = $key.'.Count'; |
||
| 174 | } |
||
| 175 | |||
| 176 | return $ar; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function MyDbFieldsAndHasOnes() |
||
| 180 | { |
||
| 181 | return |
||
| 182 | $this->retrieveDBFields('db') + |
||
| 183 | $this->retrieveDBFields('has_one'); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function MyDbFieldsAndHasOnesWithIDs() |
||
| 187 | { |
||
| 188 | $list = $this->retrieveDBFields('db'); |
||
| 189 | $hasOnes = $this->retrieveDBFields('has_one'); |
||
| 190 | foreach ($hasOnes as $field => $type) { |
||
| 191 | $fieldWithID = $field . 'ID'; |
||
| 192 | $list[$fieldWithID] = $fieldWithID; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $list; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function MyDbFieldsAndIndexes() |
||
| 199 | { |
||
| 200 | return |
||
| 201 | $this->MyDbFieldsWithDefaults() + |
||
| 202 | ['index1' => 'index1'] + |
||
| 203 | ['index2' => 'index2'] + |
||
| 204 | ['index3' => 'index3'] + |
||
| 205 | ['index4' => 'index4'] + |
||
| 206 | ['index5' => 'index5']; |
||
| 207 | } |
||
| 208 | |||
| 209 | public function MyAllFieldsWithBelongs() |
||
| 210 | { |
||
| 211 | return $this->myAllFieldsWithoutBelongs(true); |
||
| 212 | } |
||
| 213 | |||
| 214 | public function MyAllFieldsWithoutBelongs($includeBelongs = false) |
||
| 215 | { |
||
| 216 | $list = $this->MyDbFieldsWithDefaults(); |
||
| 217 | if ($includeBelongs) { |
||
| 218 | $list += $this->retrieveDBFields('belongs_to'); |
||
| 219 | } |
||
| 220 | $list += |
||
| 221 | $this->retrieveDBFields('has_one') + |
||
| 222 | $this->retrieveDBFields('has_many') + |
||
| 223 | $this->retrieveDBFields('many_many'); |
||
| 224 | if ($includeBelongs) { |
||
| 225 | $list += $this->retrieveDBFields('belongs_many_many'); |
||
| 226 | } |
||
| 227 | |||
| 228 | return $list; |
||
| 229 | } |
||
| 230 | |||
| 231 | |||
| 232 | public function IndexOptions() |
||
| 233 | { |
||
| 234 | return [ |
||
| 235 | 'true' => 'true', |
||
| 236 | 'unique("<column-name>")' => 'unique', |
||
| 237 | '[\'type\' => \'<type>\', \'value\' => \'"<column-name>"\']' => 'other' |
||
| 238 | ]; |
||
| 239 | } |
||
| 240 | |||
| 241 | public function RequiredOptions() |
||
| 242 | { |
||
| 243 | return [ |
||
| 244 | 'true' => 'true', |
||
| 245 | 'unique' => 'unique' |
||
| 246 | ]; |
||
| 247 | } |
||
| 248 | |||
| 249 | |||
| 250 | public function PossibleRelationsWithBaseClass($rootClass = '') |
||
| 251 | { |
||
| 252 | if ($rootClass) { |
||
|
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. Loading history...
|
|||
| 253 | // |
||
| 254 | } else { |
||
| 255 | $rootClass = $this->rootBaseClass; |
||
| 256 | } |
||
| 257 | $list = |
||
| 258 | [$rootClass => $rootClass] + |
||
| 259 | $this->possibleRelations(); |
||
| 260 | asort($list); |
||
| 261 | |||
| 262 | return $list; |
||
| 263 | } |
||
| 264 | |||
| 265 | protected $_classesCache = []; |
||
| 266 | |||
| 267 | /** |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public function PossibleRelations($rootClass = '') |
||
| 272 | { |
||
| 273 | if ($rootClass) { |
||
|
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. Loading history...
|
|||
| 274 | // |
||
| 275 | } else { |
||
| 276 | $rootClass = $this->rootBaseClass; |
||
| 277 | } |
||
| 278 | if (!isset($this->_classesCache[$rootClass])) { |
||
| 279 | $list = \ClassInfo::subclassesFor($rootClass); |
||
| 280 | $newList = []; |
||
| 281 | View Code Duplication | foreach ($list as $class) { |
|
|
0 ignored issues
–
show
The expression
$list of type null|array is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
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...
|
|||
| 282 | if ( |
||
|
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. Loading history...
|
|||
| 283 | $class == $rootClass || |
||
| 284 | is_subclass_of($class, 'TestOnly') || |
||
| 285 | in_array($class, $this->Config()->get('excluded_data_objects')) |
||
| 286 | ) { |
||
| 287 | //do nothing |
||
| 288 | } else { |
||
| 289 | $newList[$class] = $class; |
||
| 290 | $name = \Injector::inst()->get($class)->singular_name(); |
||
| 291 | if ($name !== $class) { |
||
| 292 | $newList[$class] .= ' ('.$name.')'; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | $this->_classesCache[$rootClass] = $newList; |
||
| 297 | } |
||
| 298 | |||
| 299 | return $this->_classesCache[$rootClass]; |
||
| 300 | } |
||
| 301 | |||
| 302 | protected $_filtersCache = []; |
||
| 303 | |||
| 304 | public function PossibleSearchFilters() |
||
| 305 | { |
||
| 306 | if (count($this->_filtersCache) === 0) { |
||
| 307 | $list = \ClassInfo::subclassesFor('SearchFilter'); |
||
| 308 | $newList = []; |
||
| 309 | foreach ($list as $class) { |
||
|
0 ignored issues
–
show
The expression
$list of type null|array is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 310 | if ($class !== 'SearchFilter') { |
||
| 311 | $newList[$class] = $class; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | $this->_filtersCache = $newList; |
||
| 315 | } |
||
| 316 | |||
| 317 | return $this->_filtersCache; |
||
| 318 | } |
||
| 319 | |||
| 320 | protected $_modelAdmins = []; |
||
| 321 | |||
| 322 | public function ModelAdminOptions() |
||
| 323 | { |
||
| 324 | if (count($this->_modelAdmins) === 0) { |
||
| 325 | $list = \ClassInfo::subclassesFor('ModelAdmin'); |
||
| 326 | $newList = []; |
||
| 327 | foreach ($list as $class) { |
||
|
0 ignored issues
–
show
The expression
$list of type null|array is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
|
|||
| 328 | if ( |
||
|
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. Loading history...
|
|||
| 329 | $class == 'ModelAdmin' || |
||
| 330 | is_subclass_of($class, 'TestOnly') |
||
| 331 | ) { |
||
| 332 | //do nothing |
||
| 333 | } else { |
||
| 334 | $newList[$class] = $class; |
||
| 335 | } |
||
| 336 | } |
||
| 337 | $newList['tba'] = 'tba'; |
||
| 338 | $this->_modelAdmins = $newList; |
||
| 339 | } |
||
| 340 | |||
| 341 | return $this->_modelAdmins; |
||
| 342 | } |
||
| 343 | |||
| 344 | |||
| 345 | public function SortOptions() |
||
| 346 | { |
||
| 347 | return [ |
||
| 348 | 'ASC' => 'ASC', |
||
| 349 | 'DESC' => 'DESC' |
||
| 350 | ]; |
||
| 351 | } |
||
| 352 | |||
| 353 | protected $_canOptions = null; |
||
| 354 | |||
| 355 | public function CanOptions() |
||
|
0 ignored issues
–
show
The return type could not be reliably inferred; please add a
@return annotation.
Our type inference engine in quite powerful, but sometimes the code does not
provide enough clues to go by. In these cases we request you to add a Loading history...
|
|||
| 356 | { |
||
| 357 | if (! $this->_canOptions) { |
||
| 358 | $ar = [ |
||
| 359 | 'one' => 'only one (useful for can create)', |
||
| 360 | 'true' => 'always', |
||
| 361 | 'false' => 'never', |
||
| 362 | 'parent' => 'use parent class', |
||
| 363 | ]; |
||
| 364 | $permissions = \Permission::get()->column('Code'); |
||
| 365 | $ar = $ar + array_combine($permissions, $permissions); |
||
| 366 | |||
| 367 | $this->_canOptions = $ar; |
||
| 368 | } |
||
| 369 | |||
| 370 | return $this->_canOptions; |
||
| 371 | } |
||
| 372 | |||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function SiteTreeList($rootClass = 'SiteTree') |
||
| 380 | { |
||
| 381 | $list = \ClassInfo::subclassesFor($rootClass); |
||
| 382 | View Code Duplication | foreach ($list as $class) { |
|
|
0 ignored issues
–
show
The expression
$list of type null|array is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
Loading history...
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...
|
|||
| 383 | if ( |
||
|
0 ignored issues
–
show
This
if statement is empty and can be removed.
This check looks for the bodies of These if (rand(1, 6) > 3) {
//print "Check failed";
} else {
print "Check succeeded";
}
could be turned into if (rand(1, 6) <= 3) {
print "Check succeeded";
}
This is much more concise to read. Loading history...
|
|||
| 384 | $class == $rootClass || |
||
| 385 | is_subclass_of($class, 'TestOnly') || |
||
| 386 | in_array($class, $this->Config()->get('excluded_data_objects')) |
||
| 387 | ) { |
||
| 388 | //do nothing |
||
| 389 | } else { |
||
| 390 | $newList[$class] = $class; |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$newList was never initialized. Although not strictly required by PHP, it is generally a good practice to add $newList = 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 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...
|
|||
| 391 | $name = \Injector::inst()->get($class)->singular_name(); |
||
| 392 | if ($name !== $class) { |
||
| 393 | $newList[$class] .= ' ('.$name.')'; |
||
| 394 | } |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | return $newList; |
||
|
0 ignored issues
–
show
The variable
$newList does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
| 399 | } |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public function AllowedChildrenOptions($rootClass = 'SiteTree') |
||
| 407 | { |
||
| 408 | return ['none' => 'none'] + $this->SiteTreeList($rootClass); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | */ |
||
| 415 | public function TrueOrFalseListWithIgnore() |
||
| 416 | { |
||
| 417 | return [ |
||
| 418 | '' => '-- ignore --' |
||
| 419 | ] + |
||
| 420 | $this->TrueOrFalseList(); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | public function TrueOrFalseList() |
||
| 428 | { |
||
| 429 | return [ |
||
| 430 | 'true' => 'YES', |
||
| 431 | 'false' => 'NO' |
||
| 432 | ]; |
||
| 433 | } |
||
| 434 | } |
||
| 435 |
This check marks private properties in classes that are never used. Those properties can be removed.