Skip to content

Commit

Permalink
Merge pull request #51 from omitobi/feature/#14-remove-static-propeties
Browse files Browse the repository at this point in the history
Feature/#14 remove static propeties
  • Loading branch information
omitobi authored May 7, 2020
2 parents 1ed0dcb + e3e6818 commit e584d29
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: php

php:
- 7.2
- 7.3
- 7.4

before_script:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
],
"require": {
"php": ">=7.4"
"php": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": "~8.0"
Expand Down
19 changes: 16 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ It helps you construct conditionals as you speak it object Oriented way.
## Minimum Requirement

- PHP 7.4
- (Older version compatibility to arrive with upcoming version of this package)
- PHP 7.2 +
- Composer

## Installation

Expand All @@ -35,7 +35,7 @@ or add to the require object of `composer.json` file with the version number:
```json
{
"require": {
"omitobisam/conditional": "^1.1"
"omitobisam/conditional": "^1.2"
}
}
```
Expand All @@ -56,6 +56,7 @@ Conditional::if(is_null($data))
->else(fn() => doThat());

```
> 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
Conditional also comes with a helper function called `conditional()` and its used like so:

Expand Down Expand Up @@ -232,3 +233,15 @@ For new feature, checkout with prefix `feature/#issueid` e.g `feature/#100-add-a
## Licence

MIT (see LICENCE file)

## Additional Information

Be aware that this package is part of a series of "The Proof of Concept".

See other packages in this series here:

- https://github.com/transprime-research/piper [A functional PHP pipe in object-oriented way]
- https://github.com/transprime-research/arrayed [A smart PHP array class object-oriented way]
- https://github.com/transprime-research/attempt [A smart PHP try...catch statement]
- https://github.com/omitobi/carbonate [A smart Carbon + Collection package]
- https://github.com/omitobi/laravel-habitue [Jsonable Http Request(er) package with Collections response]
87 changes: 41 additions & 46 deletions src/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,60 @@
*/
class Conditional
{
private static bool $truthy = false;
private $truthy = false;

private static bool $conditionsExists = false;
private $conditionsExists = false;

private static bool $ifCalled = false;
private $ifCalled = false;

private static bool $thenCalled = false;
private $thenCalled = false;

private static bool $elseCalled = false;
private $elseCalled = false;

private static bool $elseIfCalled = false;
private $elseIfCalled = false;

private static $finalValue;
private $finalValue;

private static $finalValueChanged = null;
private $finalValueChanged = null;

public static function if($condition)
{
self::setTruthy($condition);
return (new static)->on($condition);
}

public function on($condition)
{
$this->setTruthy($condition);

self::$conditionsExists = true;
$this->conditionsExists = true;

self::$ifCalled = true;
$this->ifCalled = true;

return new static;
return $this;
}

public static function setTruthy($condition)
public function setTruthy($condition)
{
if (!$condition instanceof Closure) {
self::$truthy = (bool)$condition;
$this->truthy = (bool)$condition;
} else {
self::$truthy = (bool)$condition();
$this->truthy = (bool)$condition();
}
}

public function else($action)
{
if (!self::$thenCalled) {
if (!$this->thenCalled) {
throw new InvalidConditionOrderException(
'then() must be called before calling else()'
);
}

$this->toggleTruthy();

self::$conditionsExists = true;
$this->conditionsExists = true;

self::$elseCalled = true;
$this->elseCalled = true;

return $this->then($action)->value();
}
Expand All @@ -73,24 +78,26 @@ public function then($action)
);
}

if (self::$truthy) {
if ($this->truthy) {

if ($this->isExceptionClass($action)) {
throw $action;
}

if (!$this->canBeCalled($action)) {
$action = fn() => $action;
$action = function () use ($action) {
return $action;
};
}

self::$finalValue = $action();
$this->finalValue = $action();

self::$finalValueChanged = true;
$this->finalValueChanged = true;
}

self::$thenCalled = true;
$this->thenCalled = true;

self::$conditionsExists = false;
$this->conditionsExists = false;

return $this;
}
Expand All @@ -104,17 +111,17 @@ public function elseIf($condition)
);
}

self::$conditionsExists = true;
$this->conditionsExists = true;

if (self::$truthy) {
if ($this->truthy) {
$this->toggleTruthy();

return $this;
}

self::setTruthy($condition);
$this->setTruthy($condition);

self::$elseIfCalled = true;
$this->elseIfCalled = true;

return $this;
}
Expand All @@ -126,19 +133,19 @@ private function isExceptionClass($action)

public function value()
{
return self::$finalValue;
return $this->finalValue;
}

private function allowElseIf()
{
return self::$thenCalled &&
!self::$conditionsExists &&
!self::$elseCalled;
return $this->thenCalled &&
!$this->conditionsExists &&
!$this->elseCalled;
}

private function allowThen()
{
return self::$conditionsExists && (self::$ifCalled || self::$elseIfCalled);
return $this->conditionsExists && ($this->ifCalled || $this->elseIfCalled);
}

protected function canBeCalled($value)
Expand All @@ -151,18 +158,6 @@ protected function canBeCalled($value)

private function toggleTruthy()
{
self::$truthy = !self::$truthy;
}

public function __destruct()
{
self::$truthy = false;
self::$conditionsExists = false;
self::$ifCalled = false;
self::$thenCalled = false;
self::$elseCalled = false;
self::$elseIfCalled = false;
self::$finalValue = null;
self::$finalValueChanged = null;
$this->truthy = !$this->truthy;
}
}
56 changes: 43 additions & 13 deletions tests/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ public function testExecutionFollowsConditions()
$secondResponse = 2;

Conditional::if($firstResponse === $secondResponse)
->then(fn() => $this->assertEquals($firstResponse, $secondResponse))
->else(fn() => $this->assertNotEquals($firstResponse, $secondResponse));
->then(function () use ($firstResponse, $secondResponse) {
return $this->assertEquals($firstResponse, $secondResponse);
})
->else(function () use ($firstResponse, $secondResponse) {
return $this->assertNotEquals($firstResponse, $secondResponse);
});
}

public function testThatValueIsReceived()
Expand All @@ -62,9 +66,19 @@ public function testThatValueIsReceived()

public function testGetFunctionValue()
{
$result = Conditional::if(fn() => true)
->then(fn() => fn() => 'fn 1')
->else(fn() => fn() => 'fn 2');
$result = Conditional::if(function () {
return true;
})
->then(function () {
return function () {
return 'fn 1';
};
})
->else(function () {
return function () {
return 'fn 2';
};
});

$this->assertInstanceOf(Closure::class, $result);
}
Expand Down Expand Up @@ -181,17 +195,11 @@ public function testElseIfCannotBeChainedWithElseIf()
public function testElseIfCanBeCalledMultipleTimesInAnInstance()
{
$value = Conditional::if(false)

->then('a')

->elseIf('b' == 1)

->then('b')

->elseIf('b' !== 2)

->then('2')

->else(1);

$this->assertEquals($value, 2);
Expand All @@ -212,6 +220,26 @@ public function testConditionalHelperFunctionAcceptsThenAndElseValues()
);
}

public function testConditionalTwice()
{
$conditionally = function ($condition) {
$conditional = Conditional::if($condition);

Conditional::if(func_num_args() === 2)
->then('1')
->else('2');

return $conditional;
};

$this->assertEquals(
'b',
$conditionally(1 === 2)
->then('a')
->else('b')
);
}

// public function testIfCannotBeCalledAfterElseIf()
// {
// $this->expectException(InvalidConditionOrderException::class);
Expand All @@ -236,14 +264,16 @@ private function dd()
}
}

class Invokable {
class Invokable
{

public function __invoke($value = '')
{
return $value ?: 1;
}
}

class TestException extends \Exception {
class TestException extends \Exception
{

}

0 comments on commit e584d29

Please sign in to comment.