nkostadinov /
yii2-taxonomy
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 nkostadinov\taxonomy\components\terms; |
||
| 4 | |||
| 5 | use nkostadinov\taxonomy\models\TaxonomyDef; |
||
| 6 | use nkostadinov\taxonomy\models\TaxonomyTerms; |
||
| 7 | use Yii; |
||
| 8 | use yii\base\InvalidParamException; |
||
| 9 | use yii\db\Query; |
||
| 10 | use yii\helpers\ArrayHelper; |
||
| 11 | use yii\web\NotFoundHttpException; |
||
| 12 | |||
| 13 | class CategoryTerm extends HierarchicalTerm |
||
| 14 | { |
||
| 15 | public $templateFile = '@nkostadinov/taxonomy/migrations/template/category.php'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Assigns terms to an object. |
||
| 19 | * |
||
| 20 | * @param integer $object_id |
||
| 21 | * @param integer|array $params The ID/IDs of the term/terms that need to be assigned. Can be integer or array of integers. |
||
| 22 | * @return An array with the currently assigned TaxonomyTerms. |
||
| 23 | */ |
||
| 24 | public function addTerm($object_id, $params) |
||
| 25 | { |
||
| 26 | $result = []; |
||
| 27 | |||
| 28 | foreach (TaxonomyTerms::findAll((array) $params) as $term) { |
||
| 29 | $data['term_id'] = $term->id; |
||
|
0 ignored issues
–
show
|
|||
| 30 | $data['object_id'] = $object_id; |
||
|
0 ignored issues
–
show
The variable
$data 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...
|
|||
| 31 | |||
| 32 | if (!(new Query())->from($this->table)->where($data)->exists(CategoryTerm::getDb())) { |
||
|
0 ignored issues
–
show
The property
table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 33 | View Code Duplication | Yii::$app->db->transaction(function() use ($data, $term, &$result) { |
|
|
0 ignored issues
–
show
The variable
$data 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...
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...
|
|||
| 34 | CategoryTerm::getDb()->createCommand()->insert($this->table, $data)->execute(); |
||
|
0 ignored issues
–
show
The property
table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 35 | |||
| 36 | $term->updateCounters(['total_count' => 1]); |
||
| 37 | TaxonomyDef::updateAllCounters(['total_count' => 1], ['id' => $this->id]); |
||
| 38 | |||
| 39 | $result[] = $term; |
||
| 40 | }); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | return $result; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Removes terms from an object. |
||
| 49 | * |
||
| 50 | * @param integer $object_id The id of the object. |
||
| 51 | * @param array|integer $params An array of term IDs or term ID. |
||
| 52 | * @return An array with the TaxonomyTerms objects that were removed. |
||
| 53 | */ |
||
| 54 | public function removeTerm($object_id, $params = []) |
||
| 55 | { |
||
| 56 | $result = []; |
||
| 57 | |||
| 58 | foreach(TaxonomyTerms::findAll((array) $params) as $term) { |
||
| 59 | $data['term_id'] = $term->id; |
||
|
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = 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...
|
|||
| 60 | $data['object_id'] = $object_id; |
||
|
0 ignored issues
–
show
The variable
$data 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...
|
|||
| 61 | |||
| 62 | $query = new Query(); |
||
| 63 | if ($query->from($this->table)->where($data)->exists($this->getDb())) { |
||
|
0 ignored issues
–
show
The property
table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 64 | View Code Duplication | Yii::$app->db->transaction(function() use ($data, $term, &$result) { |
|
|
0 ignored issues
–
show
The variable
$data 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...
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...
|
|||
| 65 | $this->getDb()->createCommand()->delete($this->table, $data)->execute(); |
||
|
0 ignored issues
–
show
The property
table does not exist on object<nkostadinov\taxon...nts\terms\CategoryTerm>. Since you implemented __get, maybe consider adding a @property annotation.
Since your code implements the magic getter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
If the property has read access only, you can use the @property-read annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 66 | |||
| 67 | $term->updateCounters(['total_count' => -1]); |
||
| 68 | Taxonomydef::updateAllCounters(['total_count' => -1], ['id' => $this->id]); |
||
| 69 | |||
| 70 | $result[] = $term; |
||
| 71 | }); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return $result; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Overwrites the existing terms of the object, but only with the children of the given parents. |
||
| 80 | * |
||
| 81 | * $params is an array in the form [$parent_id => [$children], $parent_id2 => [$children2]]. |
||
| 82 | * The children of those parents will replace the existing terms where the given object is assigned. |
||
| 83 | * |
||
| 84 | * @param integer $object_id The object id whose terms are changed. |
||
| 85 | * @param array $params The replacement |
||
| 86 | * @return An array with the TaxonomyTerms set |
||
| 87 | */ |
||
| 88 | public function setTerms($object_id, $params = []) |
||
| 89 | { |
||
| 90 | $result = []; |
||
| 91 | |||
| 92 | foreach (TaxonomyTerms::findAll(array_keys($params)) as $parent) { |
||
| 93 | $parentsChildren = $this->getChildren($parent->id); |
||
| 94 | $this->removeTerm($object_id, ArrayHelper::getColumn($parentsChildren, 'id')); |
||
| 95 | $result = array_merge($result, $this->addTerm($object_id, $params[$parent->id])); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $result; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Creates a new category with the following specifics: |
||
| 103 | * - If $parent is string - creates a root category without any children. |
||
| 104 | * - If parent is integer (meaning an id of a term) and: |
||
| 105 | * - $children is string - creates new category and assigns it to that parent; |
||
| 106 | * - $children is array of strings - creates new categories and assigns them to that parent; |
||
| 107 | * |
||
| 108 | * @param string|integer $parent |
||
| 109 | * @param string|array $children |
||
| 110 | * @return TaxonomyTerms|array Returns the objects created |
||
| 111 | * @throws InvalidParamException If a parameter of a wrong type is given |
||
| 112 | * @throws NotFoundHttpException If a parent with a given id is not found |
||
| 113 | */ |
||
| 114 | public function createCategory($parent, $children = []) |
||
| 115 | { |
||
| 116 | if (is_string($parent)) { |
||
| 117 | return $this->getTaxonomyTerm($parent); |
||
| 118 | } |
||
| 119 | |||
| 120 | if (!is_int($parent)) { |
||
| 121 | throw new InvalidParamException('$parent must be integer!'); |
||
|
0 ignored issues
–
show
The class
yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 122 | } |
||
| 123 | |||
| 124 | if (!is_string($children) && !is_array($children)) { |
||
| 125 | throw new InvalidParamException('$children must be of type string or array!'); |
||
|
0 ignored issues
–
show
The class
yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 126 | } |
||
| 127 | |||
| 128 | if (!TaxonomyTerms::find()->where(['id' => $parent])->exists()) { |
||
| 129 | throw new NotFoundHttpException("Parent with id '$id' does not exist!"); |
||
| 130 | } |
||
| 131 | |||
| 132 | $children = (array) $children; |
||
| 133 | $result = []; |
||
| 134 | foreach ($children as $child) { |
||
| 135 | Yii::$app->db->transaction(function() use ($parent, $child, &$result) { |
||
| 136 | if (!is_string($child)) { |
||
| 137 | throw new InvalidParamException('$children must contain only string values!'); |
||
|
0 ignored issues
–
show
The class
yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead. Loading history...
|
|||
| 138 | } |
||
| 139 | |||
| 140 | $term = $this->getTaxonomyTerm($child); |
||
| 141 | $term->parent_id = $parent; |
||
| 142 | $term->save(false); |
||
| 143 | |||
| 144 | $result[] = $term; |
||
| 145 | }); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $result; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param integer $termId |
||
| 153 | * @return TaxonomyTerms |
||
| 154 | */ |
||
| 155 | public function getParent($termId) |
||
| 156 | { |
||
| 157 | $childTerm = TaxonomyTerms::findOne($termId); |
||
| 158 | if (!$childTerm || is_null($childTerm->parent_id)) { |
||
| 159 | return null; |
||
| 160 | } |
||
| 161 | |||
| 162 | return TaxonomyTerms::findOne(['id' => $childTerm->parent_id]); |
||
|
0 ignored issues
–
show
The expression
\nkostadinov\taxonomy\mo...childTerm->parent_id)); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 162 which is incompatible with the return type documented by nkostadinov\taxonomy\com...CategoryTerm::getParent of type nkostadinov\taxonomy\models\TaxonomyTerms|null.
Loading history...
|
|||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param integer $termId |
||
| 167 | * @return boolean |
||
| 168 | */ |
||
| 169 | public function hasParent($termId) |
||
| 170 | { |
||
| 171 | return $this->getParent($termId) != null; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param integer $termId |
||
| 176 | * @return array An array of TaxonomyTerms |
||
| 177 | */ |
||
| 178 | public function getChildren($termId) |
||
| 179 | { |
||
| 180 | return TaxonomyTerms::findAll(['parent_id' => $termId]); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param integer $termId |
||
| 185 | * @return boolean |
||
| 186 | */ |
||
| 187 | public function hasChildren($termId) |
||
| 188 | { |
||
| 189 | return TaxonomyTerms::find() |
||
| 190 | ->where("parent_id = $termId") |
||
| 191 | ->exists(self::getDb()); |
||
| 192 | } |
||
| 193 | } |
||
| 194 |
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.