rhosocial /
yii2-base-models
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 | /** |
||
| 4 | * _ __ __ _____ _____ ___ ____ _____ |
||
| 5 | * | | / // // ___//_ _// || __||_ _| |
||
| 6 | * | |/ // /(__ ) / / / /| || | | | |
||
| 7 | * |___//_//____/ /_/ /_/ |_||_| |_| |
||
| 8 | * @link https://vistart.me/ |
||
| 9 | * @copyright Copyright (c) 2016 - 2017 vistart |
||
| 10 | * @license https://vistart.me/license/ |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace rhosocial\base\models\traits; |
||
| 14 | |||
| 15 | use Yii; |
||
| 16 | use yii\base\ModelEvent; |
||
| 17 | use yii\caching\Cache; |
||
| 18 | use yii\caching\TagDependency; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * This trait must be used in class extended from ActiveRecord. The ActiveRecord |
||
| 22 | * supports [[\yii\db\ActiveRecord]], [[\yii\mongodb\ActiveRecord]], [[\yii\redis\ActiveRecord]]. |
||
| 23 | * @property array $entityRules |
||
| 24 | * @property array $entityBehaviors |
||
| 25 | * @version 1.0 |
||
| 26 | * @author vistart <[email protected]> |
||
| 27 | */ |
||
| 28 | trait EntityTrait |
||
| 29 | { |
||
| 30 | use GUIDTrait, IDTrait, IPTrait, TimestampTrait, SubsidiaryTrait; |
||
| 31 | |||
| 32 | private $entityLocalRules = []; |
||
| 33 | private $entityLocalBehaviors = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string cache key and tag prefix. the prefix is usually set to full |
||
| 37 | * qualified class name. |
||
| 38 | */ |
||
| 39 | public $cachePrefix = ''; |
||
| 40 | public static $eventNewRecordCreated = 'newRecordCreated'; |
||
| 41 | public static $cacheKeyEntityRules = 'entity_rules'; |
||
| 42 | public static $cacheTagEntityRules = 'tag_entity_rules'; |
||
| 43 | public static $cacheKeyEntityBehaviors = 'entity_behaviors'; |
||
| 44 | public static $cacheTagEntityBehaviors = 'tag_entity_behaviors'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string cache component id. |
||
| 48 | */ |
||
| 49 | public $cacheId = 'cache'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var boolean Determines to skip initialization. |
||
| 53 | */ |
||
| 54 | public $skipInit = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string the name of query class or sub-class. |
||
| 58 | */ |
||
| 59 | public $queryClass; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return \static New self without any initializations. |
||
| 63 | */ |
||
| 64 | 378 | public static function buildNoInitModel() |
|
| 65 | { |
||
| 66 | 378 | return new static(['skipInit' => true]); |
|
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Populate and return the entity rules. |
||
| 71 | * You should call this function in your extended class and merge the result |
||
| 72 | * with your rules, instead of overriding it, unless you know the |
||
| 73 | * consequences. |
||
| 74 | * The classical rules are like following: |
||
| 75 | * [ |
||
| 76 | * ['guid', 'required'], |
||
| 77 | * ['guid', 'unique'], |
||
| 78 | * ['guid', 'string', 'max' => 36], |
||
| 79 | * |
||
| 80 | * ['id', 'required'], |
||
| 81 | * ['id', 'unique'], |
||
| 82 | * ['id', 'string', 'max' => 4], |
||
| 83 | * |
||
| 84 | * ['created_at', 'safe'], |
||
| 85 | * ['updated_at', 'safe'], |
||
| 86 | * |
||
| 87 | * ['ip_type', 'in', 'range' => [4, 6]], |
||
| 88 | * ['ip', 'number', 'integerOnly' => true, 'min' => 0], |
||
| 89 | * ] |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | 361 | public function rules() |
|
| 93 | { |
||
| 94 | 361 | return $this->getEntityRules(); |
|
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Populate and return the entity behaviors. |
||
| 99 | * You should call this function in your extended class and merge the result |
||
| 100 | * with your behaviors, instead of overriding it, unless you know the |
||
| 101 | * consequences. |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | 392 | public function behaviors() |
|
| 105 | { |
||
| 106 | 392 | return $this->getEntityBehaviors(); |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get cache component. If cache component is not configured, Yii::$app->cache |
||
| 111 | * will be given. |
||
| 112 | * @return Cache cache component. |
||
| 113 | */ |
||
| 114 | 392 | protected function getCache() |
|
| 115 | { |
||
| 116 | 392 | $cacheId = $this->cacheId; |
|
| 117 | 392 | return empty($cacheId) ? Yii::$app->cache : Yii::$app->$cacheId; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get entity rules cache key. |
||
| 122 | * @return string cache key. |
||
| 123 | */ |
||
| 124 | 361 | public function getEntityRulesCacheKey() |
|
| 125 | { |
||
| 126 | 361 | return static::class . $this->cachePrefix . static::$cacheKeyEntityRules; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get entity rules cache tag. |
||
| 131 | * @return string cache tag. |
||
| 132 | */ |
||
| 133 | 361 | public function getEntityRulesCacheTag() |
|
| 134 | { |
||
| 135 | 361 | return static::class . $this->cachePrefix . static::$cacheTagEntityRules; |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get entity rules. |
||
| 140 | * @return array rules. |
||
| 141 | */ |
||
| 142 | 361 | public function getEntityRules() |
|
| 143 | { |
||
| 144 | 361 | $cache = $this->getCache(); |
|
| 145 | 361 | if ($cache) { |
|
| 146 | 361 | $this->entityLocalRules = $cache->get($this->getEntityRulesCacheKey()); |
|
|
0 ignored issues
–
show
|
|||
| 147 | } |
||
| 148 | 361 | if (empty($this->entityLocalRules) || !is_array($this->entityLocalRules)) { |
|
| 149 | 361 | $rules = array_merge($this->getGuidRules(), $this->getIdRules(), $this->getCreatedAtRules(), $this->getUpdatedAtRules(), $this->getExpiredAfterRules(), $this->getIpRules()); |
|
| 150 | 361 | $this->setEntityRules($rules); |
|
| 151 | } |
||
| 152 | 361 | return $this->entityLocalRules; |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set entity rules. |
||
| 157 | * @param array $rules |
||
| 158 | */ |
||
| 159 | 361 | protected function setEntityRules($rules = []) |
|
| 160 | { |
||
| 161 | 361 | $this->entityLocalRules = $rules; |
|
| 162 | 361 | $cache = $this->getCache(); |
|
| 163 | 361 | if ($cache) { |
|
| 164 | 361 | $tagDependency = new TagDependency( |
|
| 165 | 361 | ['tags' => [$this->getEntityRulesCacheTag()]] |
|
| 166 | ); |
||
| 167 | 361 | $cache->set($this->getEntityRulesCacheKey(), $rules, 0, $tagDependency); |
|
| 168 | } |
||
| 169 | 361 | } |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Get entity behaviors cache key. |
||
| 173 | * @return string cache key. |
||
| 174 | */ |
||
| 175 | 392 | public function getEntityBehaviorsCacheKey() |
|
| 176 | { |
||
| 177 | 392 | return static::class . $this->cachePrefix . static::$cacheKeyEntityBehaviors; |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get entity behaviors cache tag. |
||
| 182 | * @return string cache tag. |
||
| 183 | */ |
||
| 184 | 392 | public function getEntityBehaviorsCacheTag() |
|
| 185 | { |
||
| 186 | 392 | return static::class . $this->cachePrefix . static::$cacheTagEntityBehaviors; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the entity behaviors. |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | 392 | public function getEntityBehaviors() |
|
| 194 | { |
||
| 195 | 392 | $cache = $this->getCache(); |
|
| 196 | 392 | if ($cache) { |
|
| 197 | 392 | $this->entityLocalBehaviors = $cache->get($this->getEntityBehaviorsCacheKey()); |
|
|
0 ignored issues
–
show
It seems like
$cache->get($this->getEntityBehaviorsCacheKey()) of type * is incompatible with the declared type array of property $entityLocalBehaviors.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 198 | } |
||
| 199 | 392 | if (empty($this->entityLocalBehaviors) || !is_array($this->entityLocalBehaviors)) { |
|
| 200 | 392 | $this->setEntityBehaviors($this->getTimestampBehaviors()); |
|
| 201 | } |
||
| 202 | 392 | return $this->entityLocalBehaviors; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Set the entity behaviors. |
||
| 207 | * @param array $behaviors |
||
| 208 | */ |
||
| 209 | 392 | protected function setEntityBehaviors($behaviors) |
|
| 210 | { |
||
| 211 | 392 | $this->entityLocalBehaviors = $behaviors; |
|
| 212 | 392 | $cache = $this->getCache(); |
|
| 213 | 392 | if ($cache) { |
|
| 214 | 392 | $tagDependencyConfig = ['tags' => [$this->getEntityBehaviorsCacheTag()]]; |
|
| 215 | 392 | $tagDependency = new TagDependency($tagDependencyConfig); |
|
| 216 | 392 | $cache->set($this->getEntityBehaviorsCacheKey(), $behaviors, 0, $tagDependency); |
|
| 217 | } |
||
| 218 | 392 | } |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Reset cache key. |
||
| 222 | * @param string $cacheKey |
||
| 223 | * @param mixed $value |
||
| 224 | * @return boolean whether the value is successfully stored into cache. if |
||
| 225 | * cache component was not configured, then return false directly. |
||
| 226 | */ |
||
| 227 | 1 | public function resetCacheKey($cacheKey, $value = false) |
|
| 228 | { |
||
| 229 | 1 | $cache = $this->getCache(); |
|
| 230 | 1 | if ($cache) { |
|
| 231 | 1 | return $this->getCache()->set($cacheKey, $value); |
|
| 232 | } |
||
| 233 | return false; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Attach events associated with entity model. |
||
| 238 | */ |
||
| 239 | 392 | protected function initEntityEvents() |
|
| 240 | { |
||
| 241 | 392 | $this->on(static::EVENT_INIT, [$this, 'onInitCache']); |
|
|
0 ignored issues
–
show
The method
on does not exist on object<rhosocial\base\models\traits\EntityTrait>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when class ParentClass {
private $data = array();
public function __call($method, array $args) {
if (0 === strpos($method, 'get')) {
return $this->data[strtolower(substr($method, 3))];
}
throw new \LogicException(sprintf('Unsupported method: %s', $method));
}
}
/**
* If this class knows which fields exist, you can specify the methods here:
*
* @method string getName()
*/
class SomeClass extends ParentClass { }
Loading history...
|
|||
| 242 | 392 | $this->attachInitGUIDEvent(static::$eventNewRecordCreated); |
|
| 243 | 392 | $this->attachInitIDEvent(static::$eventNewRecordCreated); |
|
| 244 | 392 | $this->attachInitIPEvent(static::$eventNewRecordCreated); |
|
| 245 | 392 | if ($this->isNewRecord) { |
|
| 246 | 392 | $this->trigger(static::$eventNewRecordCreated); |
|
|
0 ignored issues
–
show
The method
trigger does not exist on object<rhosocial\base\models\traits\EntityTrait>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when class ParentClass {
private $data = array();
public function __call($method, array $args) {
if (0 === strpos($method, 'get')) {
return $this->data[strtolower(substr($method, 3))];
}
throw new \LogicException(sprintf('Unsupported method: %s', $method));
}
}
/**
* If this class knows which fields exist, you can specify the methods here:
*
* @method string getName()
*/
class SomeClass extends ParentClass { }
Loading history...
|
|||
| 247 | } |
||
| 248 | 392 | $this->on(static::EVENT_AFTER_FIND, [$this, 'onRemoveExpired']); |
|
|
0 ignored issues
–
show
The method
on does not exist on object<rhosocial\base\models\traits\EntityTrait>? Since you implemented __call, maybe consider adding a @method annotation.
If you implement This is often the case, when class ParentClass {
private $data = array();
public function __call($method, array $args) {
if (0 === strpos($method, 'get')) {
return $this->data[strtolower(substr($method, 3))];
}
throw new \LogicException(sprintf('Unsupported method: %s', $method));
}
}
/**
* If this class knows which fields exist, you can specify the methods here:
*
* @method string getName()
*/
class SomeClass extends ParentClass { }
Loading history...
|
|||
| 249 | 392 | } |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Initialize the cache prefix. |
||
| 253 | * @param ModelEvent $event |
||
| 254 | */ |
||
| 255 | 392 | public function onInitCache($event) |
|
| 256 | { |
||
| 257 | 392 | $sender = $event->sender; |
|
| 258 | 392 | $data = $event->data; |
|
| 259 | 392 | if (isset($data['prefix'])) { |
|
| 260 | $sender->cachePrefix = $data['prefix']; |
||
| 261 | } else { |
||
| 262 | 392 | $sender->cachePrefix = $sender::className(); |
|
| 263 | } |
||
| 264 | 392 | } |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Record warnings. |
||
| 268 | */ |
||
| 269 | protected function recordWarnings() |
||
| 270 | { |
||
| 271 | if (YII_ENV !== YII_ENV_PROD || YII_DEBUG) { |
||
| 272 | Yii::warning($this->errors); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get guid or id. if neither disabled, return null. |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 19 | public function __toString() |
|
| 281 | { |
||
| 282 | 19 | if (is_string($this->guidAttribute) && !empty($this->guidAttribute)) { |
|
| 283 | 16 | return $this->getGUID(); |
|
| 284 | } |
||
| 285 | 3 | if (is_string($this->idAttribute) && !empty($this->idAttribute)) { |
|
| 286 | 3 | return $this->getId(); |
|
| 287 | } |
||
| 288 | return parent::__toString(); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @inheritdoc |
||
| 293 | * ------------- |
||
| 294 | * if enable `$idAttribute` and $row[$idAttribute] set, the `idPreassigned` |
||
| 295 | * will be assigned to true. |
||
| 296 | */ |
||
| 297 | 216 | public static function instantiate($row) |
|
| 298 | { |
||
| 299 | 216 | $self = static::buildNoInitModel(); |
|
| 300 | 216 | if (isset($self->idAttribute) && isset($row[$self->idAttribute])) { |
|
| 301 | 216 | $model = new static(['idPreassigned' => true]); |
|
| 302 | } else { |
||
| 303 | $model = new static; |
||
| 304 | } |
||
| 305 | 216 | return $model; |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * unset entity attributes. |
||
| 310 | * @return array result. |
||
| 311 | */ |
||
| 312 | 1 | public function unsetSelfFields() |
|
| 313 | { |
||
| 314 | 1 | return static::unsetFields($this->attributes, $this->enabledFields()); |
|
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * unset fields of array. |
||
| 319 | * @param array $array |
||
| 320 | * @param array $fields |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 1 | public static function unsetFields($array, $fields = null) |
|
| 324 | { |
||
| 325 | 1 | if (!is_array($array)) { |
|
| 326 | $fields = []; |
||
| 327 | } |
||
| 328 | 1 | foreach ($array as $key => $value) { |
|
| 329 | 1 | if (is_string($key) && in_array($key, $fields)) { |
|
| 330 | 1 | unset($array[$key]); |
|
| 331 | } |
||
| 332 | } |
||
| 333 | 1 | return $array; |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Get enabled fields. |
||
| 338 | * @return string[] |
||
| 339 | */ |
||
| 340 | 136 | public function enabledFields() |
|
| 341 | { |
||
| 342 | 136 | return array_merge( |
|
| 343 | 136 | (is_string($this->guidAttribute) && !empty($this->guidAttribute)) ? [$this->guidAttribute] : [], |
|
| 344 | 136 | (is_string($this->idAttribute) && !empty($this->idAttribute)) ? [$this->idAttribute] : [], |
|
| 345 | 136 | $this->enabledTimestampFields(), |
|
| 346 | 136 | $this->enabledIPFields() |
|
| 347 | ); |
||
| 348 | } |
||
| 349 | } |
||
| 350 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..