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

includes/Model/Content.php (2 issues)

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
/**
5
 * parent class to provide the content model
6
 *
7
 * @since 3.3.0
8
 *
9
 * @package Redaxscript
10
 * @category Model
11
 * @author Henry Ruhs
12
 */
13
14
class Content
15
{
16
	/**
17
	 * get the content table by alias
18
	 *
19
	 * @since 3.3.0
20
	 *
21
	 * @param string $contentAlias alias of the content
22
	 *
23
	 * @return string|null
24
	 */
25
26
	public function getTableByAlias(string $contentAlias = null) : ?string
27
	{
28
		if ($contentAlias)
29
		{
30
			$categoryModel = new Category();
31
			if ($categoryModel->getByAlias($contentAlias)->id)
32
			{
33
				return 'categories';
34
			}
35
			$articleModel = new Article();
36
			if ($articleModel->getByAlias($contentAlias)->id)
37
			{
38
				return 'articles';
39
			}
40
		}
41
		return null;
42
	}
43
44
	/**
45
	 * get the content by table and id
46
	 *
47
	 * @since 4.0.0
48
	 *
49
	 * @param string $table name of the table
50
	 * @param int $contentId identifier of the content
51
	 *
52
	 * @return object|null
53
	 */
54
55
	public function getByTableAndId(string $table = null, int $contentId = null) : ?object
56
	{
57
		if ($contentId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $contentId 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...
58
		{
59
			if ($table === 'categories')
60
			{
61
				$categoryModel = new Category();
62
				return $categoryModel->getById($contentId);
63
			}
64
			if ($table === 'articles')
65
			{
66
				$articleModel = new Article();
67
				return $articleModel->getById($contentId);
68
			}
69
		}
70
		return null;
71
	}
72
73
	/**
74
	 * get the content route by table and id
75
	 *
76
	 * @since 3.3.0
77
	 *
78
	 * @param string $table name of the table
79
	 * @param int $contentId identifier of the content
80
	 *
81
	 * @return string|null
82
	 */
83
84
	public function getRouteByTableAndId(string $table = null, int $contentId = null) : ?string
85
	{
86
		if ($contentId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $contentId 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...
87
		{
88
			if ($table === 'categories')
89
			{
90
				$categoryModel = new Category();
91
				return $categoryModel->getRouteById($contentId);
92
			}
93
			if ($table === 'articles')
94
			{
95
				$articleModel = new Article();
96
				return $articleModel->getRouteById($contentId);
97
			}
98
			if ($table === 'comments')
99
			{
100
				$commentModel = new Comment();
101
				return $commentModel->getRouteById($contentId);
102
			}
103
		}
104
		return null;
105
	}
106
}
107