Skip to content

Commit 827091e

Browse files
lucs7JohnVillalovos
authored andcommitted
fix: Update type comparisons to strict equality for CustomAttributeTypes and CustomAttributeCategory
fixes phpstan issues for wrong type comparisson
1 parent f780cb1 commit 827091e

9 files changed

Lines changed: 37 additions & 53 deletions

File tree

Domain/Access/AttributeFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public static function Create($entityTableAndColumn, $attributes)
2727

2828
$idEquals = new SqlFilterEquals($attributeId, $attribute->Id());
2929
$f->AppendSql('LEFT JOIN `' . TableNames::CUSTOM_ATTRIBUTE_VALUES . '` `a' . $id . '` ON `a0`.`entity_id` = `a' . $id . '`.`entity_id` ');
30-
if ($attribute->Type() == CustomAttributeTypes::MULTI_LINE_TEXTBOX || $attribute->Type() == CustomAttributeTypes::SINGLE_LINE_TEXTBOX) {
30+
if ((int)$attribute->Type() === CustomAttributeTypes::MULTI_LINE_TEXTBOX || (int)$attribute->Type() === CustomAttributeTypes::SINGLE_LINE_TEXTBOX) {
3131
$attributeFragment->_And($idEquals->_And(new SqlFilterLike($attributeValue, $attribute->Value())));
32-
} elseif ($attribute->Type() == CustomAttributeTypes::CHECKBOX && $attribute->Value() == '0') {
32+
} elseif ((int)$attribute->Type() === CustomAttributeTypes::CHECKBOX && $attribute->Value() == '0') {
3333
$attributeFragment->_And(new SqlFilterFreeForm('NOT EXISTS (SELECT 1 FROM `' . TableNames::CUSTOM_ATTRIBUTE_VALUES . '` `b` WHERE `b`.`entity_id` = `a0`.`entity_id` AND `b`.`custom_attribute_id` = ' . $id . ')'));
3434
} else {
3535
$attributeFragment->_And($idEquals->_And(new SqlFilterEquals($attributeValue, $attribute->Value())));

Domain/CustomAttribute.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function __construct(
308308
$this->category = $category;
309309
$this->SetRegex($regex);
310310
$this->required = $required;
311-
if ($category != CustomAttributeCategory::RESERVATION) {
311+
if ((int)$category !== CustomAttributeCategory::RESERVATION) {
312312
$this->entityIds = is_array($entityIds) ? $entityIds : ($entityIds);
313313
}
314314
$this->adminOnly = $adminOnly;
@@ -452,7 +452,7 @@ public function Update($label, $regex, $required, $possibleValues, $sortOrder, $
452452
$this->SetRegex($regex);
453453
$this->required = $required;
454454

455-
if ($this->category != CustomAttributeCategory::RESERVATION) {
455+
if ((int)$this->category !== CustomAttributeCategory::RESERVATION) {
456456
$entityIds = is_array($entityIds) ? $entityIds : [$entityIds];
457457
$removed = array_diff($this->entityIds, $entityIds);
458458
$added = array_diff($entityIds, $this->entityIds);
@@ -503,7 +503,7 @@ public function WithEntityDescriptions($entityDescriptions)
503503
*/
504504
public function WithSecondaryEntities($category, $entityIds, $entityDescriptions = null)
505505
{
506-
if ($this->category != CustomAttributeCategory::RESERVATION && $this->category != CustomAttributeCategory::RESOURCE) {
506+
if ((int)$this->category !== CustomAttributeCategory::RESERVATION && (int)$this->category !== CustomAttributeCategory::RESOURCE) {
507507
return;
508508
}
509509

Pages/Admin/ManageAttributesPage.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public function GetLabel();
1313

1414
/**
1515
* @abstract
16-
* return int|CustomAttributeTypes
16+
* return CustomAttributeTypes
1717
*/
1818
public function GetType();
1919

2020
/**
21-
* return int|CustomAttributeCategory
21+
* return CustomAttributeCategory
2222
*/
2323
public function GetCategory();
2424

@@ -43,7 +43,7 @@ public function GetEntityIds();
4343
public function GetPossibleValues();
4444

4545
/**
46-
* return int|CustomAttributeCategory
46+
* return CustomAttributeCategory
4747
*/
4848
public function GetRequestedCategory();
4949

@@ -63,7 +63,7 @@ public function GetIsAdminOnly();
6363
public function BindAttributes($attributes);
6464

6565
/**
66-
* @param $categoryId int|CustomAttributeCategory
66+
* @param $categoryId CustomAttributeCategory
6767
*/
6868
public function SetCategory($categoryId);
6969

@@ -78,7 +78,7 @@ public function GetAttributeId();
7878
public function GetSecondaryEntityIds();
7979

8080
/**
81-
* @return CustomAttributeCategory|int|null
81+
* @return CustomAttributeCategory|null
8282
*/
8383
public function GetSecondaryCategory();
8484

Pages/Admin/ManageResourcesPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ public function AsFilter($customAttributes)
13231323

13241324
$idEquals = new SqlFilterEquals($attributeId, $id);
13251325
$f->AppendSql('LEFT JOIN `' . TableNames::CUSTOM_ATTRIBUTE_VALUES . '` `a' . $id . '` ON `a0`.`entity_id` = `a' . $id . '`.`entity_id` ');
1326-
if ($attribute->Type() == CustomAttributeTypes::MULTI_LINE_TEXTBOX || $attribute->Type() == CustomAttributeTypes::SINGLE_LINE_TEXTBOX) {
1326+
if ((int)$attribute->Type() === CustomAttributeTypes::MULTI_LINE_TEXTBOX || (int)$attribute->Type() === CustomAttributeTypes::SINGLE_LINE_TEXTBOX) {
13271327
$attributeFragment->_And($idEquals->_And(new SqlFilterLike($attributeValue, $value)));
13281328
} else {
13291329
$attributeFragment->_And($idEquals->_And(new SqlFilterEquals($attributeValue, $value)));

WebServices/Controllers/AttributeSaveController.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,26 @@ private function ValidateRequest($request)
169169
$errors[] = 'label is required';
170170
}
171171

172-
if ($request->type != CustomAttributeTypes::CHECKBOX &&
173-
$request->type != CustomAttributeTypes::MULTI_LINE_TEXTBOX &&
174-
$request->type != CustomAttributeTypes::SELECT_LIST &&
175-
$request->type != CustomAttributeTypes::SINGLE_LINE_TEXTBOX
172+
if ((int)$request->type !== CustomAttributeTypes::CHECKBOX &&
173+
(int)$request->type !== CustomAttributeTypes::MULTI_LINE_TEXTBOX &&
174+
(int)$request->type !== CustomAttributeTypes::SELECT_LIST &&
175+
(int)$request->type !== CustomAttributeTypes::SINGLE_LINE_TEXTBOX &&
176+
(int)$request->type !== CustomAttributeTypes::DATETIME
176177
) {
177178
$errors[] = sprintf(
178-
'type is invalid. Allowed values for type: %s (checkbox), %s (multi line), %s (select list), %s (single line)',
179+
'type is invalid. Allowed values for type: %s (checkbox), %s (multi line), %s (select list), %s (single line), %s (datetime)',
179180
CustomAttributeTypes::CHECKBOX,
180181
CustomAttributeTypes::MULTI_LINE_TEXTBOX,
181182
CustomAttributeTypes::SELECT_LIST,
182-
CustomAttributeTypes::SINGLE_LINE_TEXTBOX
183+
CustomAttributeTypes::SINGLE_LINE_TEXTBOX,
184+
CustomAttributeTypes::DATETIME
183185
);
184186
}
185187

186-
if ($request->categoryId != CustomAttributeCategory::RESERVATION &&
187-
$request->categoryId != CustomAttributeCategory::RESOURCE &&
188-
$request->categoryId != CustomAttributeCategory::RESOURCE_TYPE &&
189-
$request->categoryId != CustomAttributeCategory::USER
188+
if ((int)$request->categoryId !== CustomAttributeCategory::RESERVATION &&
189+
(int)$request->categoryId !== CustomAttributeCategory::RESOURCE &&
190+
(int)$request->categoryId !== CustomAttributeCategory::RESOURCE_TYPE &&
191+
(int)$request->categoryId !== CustomAttributeCategory::USER
190192
) {
191193
$errors[] = sprintf(
192194
'categoryId is invalid. Allowed values for category: %s (reservation), %s (resource), %s (resource type), %s (user)',
@@ -197,15 +199,15 @@ private function ValidateRequest($request)
197199
);
198200
}
199201

200-
if ($request->type == CustomAttributeTypes::SELECT_LIST && empty($request->possibleValues)) {
202+
if ((int)$request->type === CustomAttributeTypes::SELECT_LIST && empty($request->possibleValues)) {
201203
$errors[] = 'possibleValues is required when the type is a select list';
202204
}
203205

204-
if ($request->type != CustomAttributeTypes::SELECT_LIST && !empty($request->possibleValues)) {
206+
if ((int)$request->type !== CustomAttributeTypes::SELECT_LIST && !empty($request->possibleValues)) {
205207
$errors[] = 'possibleValues is only valid when the type is a select list';
206208
}
207209

208-
if ($request->categoryId == CustomAttributeCategory::RESERVATION && !empty($request->appliesToIds)) {
210+
if ((int)$request->categoryId === CustomAttributeCategory::RESERVATION && !empty($request->appliesToIds)) {
209211
$errors[] = 'appliesToId is not valid when the type is reservation';
210212
}
211213

lib/Application/Attributes/AttributeService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ public function GetReservationAttributes(UserSession $userSession, ReservationVi
245245
$secondaryCategory = $attribute->SecondaryCategory();
246246
if (empty($secondaryCategory) ||
247247
(
248-
$secondaryCategory == CustomAttributeCategory::USER &&
248+
(int)$secondaryCategory === CustomAttributeCategory::USER &&
249249
$this->AvailableForUser($userSession, $requestedUserId, $secondaryCategory, $attribute) ||
250-
(($secondaryCategory == CustomAttributeCategory::RESOURCE || $secondaryCategory == CustomAttributeCategory::RESOURCE_TYPE)
250+
(((int)$secondaryCategory === CustomAttributeCategory::RESOURCE || (int)$secondaryCategory === CustomAttributeCategory::RESOURCE_TYPE)
251251
&& $this->AvailableForResource($userSession, $secondaryCategory, $attribute, $requestedResourceIds))
252252
)
253253
) {
@@ -335,8 +335,8 @@ private function AvailableForUser(UserSession $userSession, $requestedUserId, $s
335335
*/
336336
private function AvailableForResource($userSession, $secondaryCategory, $attribute, $requestedResourceIds)
337337
{
338-
if ($secondaryCategory == CustomAttributeCategory::RESOURCE || $secondaryCategory == CustomAttributeCategory::RESOURCE_TYPE) {
339-
if ($secondaryCategory == CustomAttributeCategory::RESOURCE) {
338+
if ((int)$secondaryCategory === CustomAttributeCategory::RESOURCE || (int)$secondaryCategory === CustomAttributeCategory::RESOURCE_TYPE) {
339+
if ((int)$secondaryCategory === CustomAttributeCategory::RESOURCE) {
340340
$applies = array_intersect($attribute->SecondaryEntityIds(), $requestedResourceIds);
341341
$allowed = array_intersect($attribute->SecondaryEntityIds(), array_keys($this->GetAllowedResources($userSession)));
342342

lib/Application/Reservation/Validation/CustomAttributeValidationRule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ public function Validate($reservationSeries, $retryParameters)
3838
$secondaryCategory = $invalidAttribute->Attribute->SecondaryCategory();
3939
$secondaryEntityIds = $invalidAttribute->Attribute->SecondaryEntityIds();
4040

41-
if ($secondaryCategory == CustomAttributeCategory::USER && !in_array($reservationSeries->UserId(), $secondaryEntityIds)) {
41+
if ((int)$secondaryCategory === CustomAttributeCategory::USER && !in_array($reservationSeries->UserId(), $secondaryEntityIds)) {
4242
// the attribute applies to a different user
4343
continue;
4444
}
45-
if ($secondaryCategory == CustomAttributeCategory::RESOURCE && count(array_intersect($secondaryEntityIds, $reservationSeries->AllResourceIds())) == 0) {
45+
if ((int)$secondaryCategory === CustomAttributeCategory::RESOURCE && count(array_intersect($secondaryEntityIds, $reservationSeries->AllResourceIds())) == 0) {
4646
// the attribute is not for a resource that is being booked
4747
continue;
4848
}
49-
if ($secondaryCategory == CustomAttributeCategory::RESOURCE_TYPE) {
49+
if ((int)$secondaryCategory === CustomAttributeCategory::RESOURCE_TYPE) {
5050
$appliesToResourceType = false;
5151
foreach ($reservationSeries->AllResources() as $resource) {
5252
if ($appliesToResourceType) {

lib/Application/Schedule/ScheduleResourceFilter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ public function FilterResources($resources, IResourceRepository $resourceReposit
160160
}
161161

162162
/**
163-
* @param Attribute[] $attributes
163+
* @param LBAttribute[] $attributes
164164
* @param int $attributeId
165-
* @return null|Attribute
165+
* @return null|LBAttribute
166166
*/
167167
private function GetAttribute($attributes, $attributeId)
168168
{
@@ -176,7 +176,7 @@ private function GetAttribute($attributes, $attributeId)
176176

177177
/**
178178
* @param AttributeValue $attribute
179-
* @param Attribute $value
179+
* @param LBAttribute $value
180180
* @return bool
181181
*/
182182
private function AttributeValueMatches($attribute, $value)
@@ -185,7 +185,7 @@ private function AttributeValueMatches($attribute, $value)
185185
return false;
186186
}
187187

188-
if ($value->Type() == CustomAttributeTypes::SINGLE_LINE_TEXTBOX || $value->Type() == CustomAttributeTypes::MULTI_LINE_TEXTBOX) {
188+
if ((int)$value->Type() == CustomAttributeTypes::SINGLE_LINE_TEXTBOX || (int)$value->Type() == CustomAttributeTypes::MULTI_LINE_TEXTBOX) {
189189
return strripos($value->Value() ?? "", $attribute->Value) !== false;
190190
} elseif (is_numeric($value->Value())) {
191191
return floatval($value->Value()) == $attribute->Value;

phpstan-baseline.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,6 @@ parameters:
102102
count: 1
103103
path: lib/Application/Schedule/ReservationService.php
104104

105-
-
106-
message: '#^Call to an undefined method Attribute\:\:Id\(\)\.$#'
107-
identifier: method.notFound
108-
count: 1
109-
path: lib/Application/Schedule/ScheduleResourceFilter.php
110-
111-
-
112-
message: '#^Call to an undefined method Attribute\:\:Type\(\)\.$#'
113-
identifier: method.notFound
114-
count: 2
115-
path: lib/Application/Schedule/ScheduleResourceFilter.php
116-
117-
-
118-
message: '#^Call to an undefined method Attribute\:\:Value\(\)\.$#'
119-
identifier: method.notFound
120-
count: 4
121-
path: lib/Application/Schedule/ScheduleResourceFilter.php
122-
123105
-
124106
message: '#^PHPDoc tag @return with type Server is not subtype of native type IRestServer\|null\.$#'
125107
identifier: return.phpDocType

0 commit comments

Comments
 (0)