Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Model/Category.php (1 issue)

Check for loose comparison of integers.

Best Practice Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Model;
3
4
use function array_filter;
5
use function implode;
6
use function is_array;
7
8
/**
9
 * parent class to provide the category model
10
 *
11
 * @since 3.3.0
12
 *
13
 * @package Redaxscript
14
 * @category Model
15
 * @author Henry Ruhs
16
 */
17
18
class Category extends ContentAbstract
19
{
20
	/**
21
	 * name of the table
22
	 *
23
	 * @var string
24
	 */
25
26
	protected $_table = 'categories';
27
28
	/**
29
	 * get the category by alias
30
	 *
31
	 * @since 4.0.0
32
	 *
33
	 * @param string $categoryAlias alias of the category
34
	 *
35
	 * @return object|null
36
	 */
37
38 2
	public function getByAlias(string $categoryAlias = null) : ?object
39
	{
40 2
		$category = $this->query()->where('alias', $categoryAlias)->findOne();
41 2
		return $category ? : null;
42
	}
43
44
	/**
45
	 * get the category route by id
46
	 *
47
	 * @since 3.3.0
48
	 *
49
	 * @param int $categoryId identifier of the category
50
	 *
51
	 * @return string|null
52
	 */
53
54 3
	public function getRouteById(int $categoryId = null) : ?string
55
	{
56 3
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
57
		{
58
			$routeArray = $this
59 2
				->query()
60 2
				->tableAlias('category')
61 2
				->leftJoinPrefix('categories', 'category.parent = parent.id', 'parent')
62 2
				->select('parent.alias', 'parentAlias')
63 2
				->select('category.alias', 'categoryAlias')
64 2
				->where('category.id', $categoryId)
65 2
				->findArray();
66
67
			/* handle route */
68
69 2
			if (is_array($routeArray[0]))
70
			{
71 2
				return implode('/', array_filter($routeArray[0]));
72
			}
73
		}
74 1
		return null;
75
	}
76
}
77