Skip to content

Commit f09865e

Browse files
authored
Merge pull request #53 from omitobi/2.x
2.x
2 parents ab0c145 + 4fd0c1e commit f09865e

File tree

4 files changed

+38
-59
lines changed

4 files changed

+38
-59
lines changed

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
}
2020
],
2121
"require": {
22-
"php": ">=7.2"
22+
"php": ">=7.4"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit": "~8.0"
25+
"phpunit/phpunit": "~8.0",
26+
"symfony/var-dumper": "^6.4"
2627
},
2728
"autoload": {
2829
"psr-4": {
@@ -36,5 +37,8 @@
3637
"psr-4": {
3738
"Conditional\\Tests\\": "tests/"
3839
}
40+
},
41+
"scripts": {
42+
"test": "vendor/bin/phpunit"
3943
}
40-
}
44+
}

readme.md

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,21 @@
1313

1414
## About Conditional
1515

16-
A (optional but) fluent helper for object-oriented style of if-else statements.
16+
if-else statements in a cleaner and beautiful way.
1717

18-
It helps you construct conditionals as you speak it object Oriented way.
19-
20-
> Some use cases are not yet covered so you can default to `if() else {}` statement.
21-
22-
## Minimum Requirement
23-
24-
- PHP 7.2 +
25-
- Composer
18+
```php
19+
conditional(isset($data))
20+
->then(fn() => doThis())
21+
->else(fn() => doThat());
22+
```
2623

2724
## Installation
2825

29-
Using composer:
30-
3126
`composer require omitobisam/conditional`
3227

33-
or add to the require object of `composer.json` file with the version number:
34-
35-
```json
36-
{
37-
"require": {
38-
"omitobisam/conditional": "^1.2"
39-
}
40-
}
41-
```
28+
## Minimum Requirement
4229

43-
After this run `composer update`
30+
- PHP >=7.4
4431

4532
## Usage
4633

@@ -56,7 +43,6 @@ Conditional::if(is_null($data))
5643
->else(fn() => doThat());
5744

5845
```
59-
> PS: You can still use the old `function() { return v; }`, `fn()` is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php
6046

6147
Conditional also comes with a helper function called `conditional()` and its used like so:
6248

@@ -207,11 +193,8 @@ Conditional::if(true)
207193
->if('1'); // Don't do it except you intend to start a new and fresh if Conditional
208194
```
209195
> See: tests/ConditionalTest::testEveryIfCallCreatesNewFreshInstance test. On the last line of that test, the two conditionals are not the same.
210-
- Conditional uses `if..else` statements in implementation, how cool is that? :smile:
211196
- Conditional relies on closures to return non closure values passed to then.
212197
> In the future release it will be optional for `then` and `else` method
213-
- This project at the initial phase is a proof of concept so performance and best practices (?)
214-
> It might be part of something great in the future (especially as a Laravel helper) how cool that would be!
215198
216199
## Contributions
217200

@@ -222,7 +205,7 @@ Conditional::if(true)
222205

223206
## Development
224207

225-
For new feature, checkout with prefix `feature/#issueid` e.g `feature/#100-add-auto-deloy`
208+
For new feature, checkout with prefix `feat-#issueid` e.g `feature-#100-add-auto-deloy`
226209

227210
-
228211
- Clone this repository
@@ -236,9 +219,7 @@ MIT (see LICENCE file)
236219

237220
## Additional Information
238221

239-
Be aware that this package is part of a series of "The Proof of Concept".
240-
241-
See other packages in this series here:
222+
Other related packages:
242223

243224
- https://github.com/transprime-research/piper [A functional PHP pipe in object-oriented way]
244225
- https://github.com/transprime-research/arrayed [A smart PHP array class object-oriented way]

src/Conditional.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Conditional;
46

57
use Closure;
@@ -12,21 +14,21 @@
1214
*/
1315
class Conditional
1416
{
15-
private $truthy = false;
17+
private bool $truthy = false;
1618

17-
private $conditionsExists = false;
19+
private bool $conditionsExists = false;
1820

19-
private $ifCalled = false;
21+
private bool $ifCalled = false;
2022

21-
private $thenCalled = false;
23+
private bool $thenCalled = false;
2224

23-
private $elseCalled = false;
25+
private bool $elseCalled = false;
2426

25-
private $elseIfCalled = false;
27+
private bool $elseIfCalled = false;
2628

2729
private $finalValue;
2830

29-
private $finalValueChanged = null;
31+
private ?bool $finalValueChanged = null;
3032

3133
public static function if($condition)
3234
{
@@ -70,7 +72,7 @@ public function else($action)
7072
return $this->then($action)->value();
7173
}
7274

73-
public function then($action)
75+
public function then($action): self
7476
{
7577
if (!$this->allowThen()) {
7678
throw new InvalidConditionOrderException(
@@ -79,7 +81,6 @@ public function then($action)
7981
}
8082

8183
if ($this->truthy) {
82-
8384
if ($this->isExceptionClass($action)) {
8485
throw $action;
8586
}
@@ -103,7 +104,7 @@ public function then($action)
103104
}
104105

105106

106-
public function elseIf($condition)
107+
public function elseIf($condition): self
107108
{
108109
if (!$this->allowElseIf()) {
109110
throw new InvalidConditionOrderException(
@@ -126,37 +127,37 @@ public function elseIf($condition)
126127
return $this;
127128
}
128129

129-
private function isExceptionClass($action)
130+
private function isExceptionClass($action): bool
130131
{
131-
return is_a($action, \Exception::class);
132+
return is_a($action, \Throwable::class);
132133
}
133134

134135
public function value()
135136
{
136137
return $this->finalValue;
137138
}
138139

139-
private function allowElseIf()
140+
private function allowElseIf(): bool
140141
{
141142
return $this->thenCalled &&
142143
!$this->conditionsExists &&
143144
!$this->elseCalled;
144145
}
145146

146-
private function allowThen()
147+
private function allowThen(): bool
147148
{
148149
return $this->conditionsExists && ($this->ifCalled || $this->elseIfCalled);
149150
}
150151

151-
protected function canBeCalled($value)
152+
protected function canBeCalled($value): bool
152153
{
153154
return (
154155
(is_object($value) && method_exists($value, '__invoke')) ||
155156
($value instanceof Closure)
156157
);
157158
}
158159

159-
private function toggleTruthy()
160+
private function toggleTruthy(): void
160161
{
161162
$this->truthy = !$this->truthy;
162163
}

tests/ConditionalTest.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Conditional\Tests;
46

57
use Closure;
@@ -43,10 +45,10 @@ public function testExecutionFollowsConditions()
4345

4446
Conditional::if($firstResponse === $secondResponse)
4547
->then(function () use ($firstResponse, $secondResponse) {
46-
return $this->assertEquals($firstResponse, $secondResponse);
48+
$this->assertEquals($firstResponse, $secondResponse);
4749
})
4850
->else(function () use ($firstResponse, $secondResponse) {
49-
return $this->assertNotEquals($firstResponse, $secondResponse);
51+
$this->assertNotEquals($firstResponse, $secondResponse);
5052
});
5153
}
5254

@@ -253,15 +255,6 @@ public function testConditionalTwice()
253255
// ->value();
254256
// }
255257

256-
private function dump(...$expression)
257-
{
258-
var_dump($expression);
259-
}
260-
261-
private function dd()
262-
{
263-
die($this->dump(...func_get_args()));
264-
}
265258
}
266259

267260
class Invokable

0 commit comments

Comments
 (0)