1 | <?php |
||
2 | /** |
||
3 | * CategoryFactory.php |
||
4 | * Copyright (c) 2018 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | /** @noinspection MultipleReturnStatementsInspection */ |
||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Factory; |
||
25 | |||
26 | |||
27 | use FireflyIII\Models\Category; |
||
28 | use FireflyIII\User; |
||
29 | use Illuminate\Support\Collection; |
||
30 | use Log; |
||
31 | |||
32 | /** |
||
33 | * Class CategoryFactory |
||
34 | */ |
||
35 | class CategoryFactory |
||
36 | { |
||
37 | /** @var User */ |
||
38 | private $user; |
||
39 | |||
40 | /** |
||
41 | * Constructor. |
||
42 | * @codeCoverageIgnore |
||
43 | */ |
||
44 | public function __construct() |
||
45 | { |
||
46 | if ('testing' === config('app.env')) { |
||
47 | Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param string $name |
||
53 | * |
||
54 | * @return Category|null |
||
55 | */ |
||
56 | public function findByName(string $name): ?Category |
||
57 | { |
||
58 | $result = null; |
||
59 | /** @var Collection $collection */ |
||
60 | $collection = $this->user->categories()->get(); |
||
61 | |||
62 | // TODO no longer need to loop like this |
||
63 | |||
64 | /** @var Category $category */ |
||
65 | foreach ($collection as $category) { |
||
66 | if ($category->name === $name) { |
||
67 | $result = $category; |
||
68 | break; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $result; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param int|null $categoryId |
||
77 | * @param null|string $categoryName |
||
78 | * |
||
79 | * @return Category|null |
||
80 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
81 | */ |
||
82 | public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category |
||
83 | { |
||
84 | $categoryId = (int)$categoryId; |
||
85 | $categoryName = (string)$categoryName; |
||
86 | |||
87 | Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName)); |
||
88 | |||
89 | if ('' === $categoryName && 0 === $categoryId) { |
||
90 | return null; |
||
91 | } |
||
92 | // first by ID: |
||
93 | if ($categoryId > 0) { |
||
94 | /** @var Category $category */ |
||
95 | $category = $this->user->categories()->find($categoryId); |
||
96 | if (null !== $category) { |
||
97 | return $category; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if ('' !== $categoryName) { |
||
102 | $category = $this->findByName($categoryName); |
||
0 ignored issues
–
show
|
|||
103 | if (null !== $category) { |
||
0 ignored issues
–
show
|
|||
104 | return $category; |
||
105 | } |
||
106 | |||
107 | return Category::create( |
||
108 | [ |
||
109 | 'user_id' => $this->user->id, |
||
110 | 'name' => $categoryName, |
||
111 | ] |
||
112 | ); |
||
113 | } |
||
114 | |||
115 | return null; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param User $user |
||
120 | */ |
||
121 | public function setUser(User $user): void |
||
122 | { |
||
123 | $this->user = $user; |
||
124 | } |
||
125 | |||
126 | } |
||
127 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.