Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to modern PHP #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/*
*.lock
index.php
.phpunit.result.cache
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ $voucher = new Voucher(['code' => 'ALAN-COLE-CODE', 'claimed_by' => '', 'claimed
print $voucher; // "ALAN-COLE-CODE"
```

Any value passed on voucher creation can be get and set using `get()` and `set()` on the voucher.
Any value passed on voucher creation can be get and set using magic getters and setters

```php
$voucher->set('owner', 'Alan');
echo $voucher->get('owner'); // Alan
$voucher->setOwner('alan')
echo $voucher->getOwner() // Alan

$voucher->setIsClaimed(true);
if($voucher->getIsClaimed() === true) {
// Sorry dave I can't do that.
}
```

### Model
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"psr-4": {
"Vouchers\\": "src/Vouchers"
}
},
"require-dev": {
"phpunit/phpunit": "^10.0",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to add "php": 8 to ensure that composer would complain given any package in the future isn't compatible.

"phpstan/phpstan": "^1.10"
}
}
20 changes: 10 additions & 10 deletions src/Vouchers/Bag.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers;

Expand Down Expand Up @@ -46,7 +46,7 @@ public function __construct(\Vouchers\Voucher\Model $model = null)
*
* @return void
*/
public function fill($number = 0)
public function fill($number = 0) :void

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing is it a little off here... ideally "): void"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we had int here as a type

{
for ($i = 0; $i < $number; $i++) {
$this->add(new Voucher());
Expand Down Expand Up @@ -77,7 +77,7 @@ public function pick(callable $callback = null)
*
* @return Voucher $voucher
*/
public function pickWithCallback(callable $callback)
public function pickWithCallback(callable $callback) :Voucher
{
foreach ($this->values as $voucher) {
if ($callback($voucher)) {
Expand All @@ -96,7 +96,7 @@ public function pickWithCallback(callable $callback)
*
* @return void
*/
public function map(array $data, callable $callback)
public function map(array $data, callable $callback) :void
{
foreach ($data as $item) {
$result = $callback($item);
Expand All @@ -114,7 +114,7 @@ public function map(array $data, callable $callback)
*
* @return bool
*/
public function pickValid()
public function pickValid() :Voucher
{
$voucher = null;
foreach ($this->values as $value) {
Expand Down Expand Up @@ -144,7 +144,7 @@ public function pickValid()
*
* @return bool
*/
public function validate($code)
public function validate($code) :bool
{
foreach ($this->validators as $rule) {
$callback = $rule['callback'];
Expand All @@ -166,7 +166,7 @@ public function validate($code)
*
* @return void
*/
public function validator(callable $callback, $message = self::VALIDATION_MESSAGE)
public function validator(callable $callback, $message = self::VALIDATION_MESSAGE) :void
{
$rule = [
'message' => $message,
Expand All @@ -182,7 +182,7 @@ public function validator(callable $callback, $message = self::VALIDATION_MESSAG
*
* @return void
*/
public function add(Voucher $voucher)
public function add(Voucher $voucher) :void
{
if ($this->model) {
$this->model->validate($voucher->toArray());
Expand All @@ -196,9 +196,9 @@ public function add(Voucher $voucher)
*
* @param string $code
*
* @return string|Voucher
* @return bool|Voucher
*/
public function find($code)
public function find($code) :bool|Voucher

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be great to add boolean here too.

{
foreach ($this->values as $key => $voucher) {
if ($code == (string) $voucher) {
Expand Down
20 changes: 11 additions & 9 deletions src/Vouchers/Bag/Collection.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers\Bag;

class Collection implements \Iterator
use Iterator;

class Collection implements Iterator
{
/**
* Holds all of our 'vouchers'.
Expand All @@ -16,31 +18,31 @@ class Collection implements \Iterator
*
* @return mixed
*/
public function current()
public function current() :mixed
{
return current($this->values);
}

/**
* Returns the key of our current.
*/
public function key()
public function key() :mixed
{
return key($this->values);
}

/**
* Moves array to next array item.
*/
public function next()
public function next() :void
{
return next($this->values);
}

/**
* Reset the array.
*/
public function rewind()
public function rewind() :void
{
return reset($this->values);
}
Expand All @@ -50,7 +52,7 @@ public function rewind()
*
* @return int
*/
public function count()
public function count() :int
{
return count($this->values);
}
Expand All @@ -59,7 +61,7 @@ public function count()
* Make sure the key is a real one
* or loops will last forever.
*/
public function valid()
public function valid() :bool
{
$key = key($this->values);

Expand All @@ -71,7 +73,7 @@ public function valid()
*
* @return array
*/
public function toArray()
public function toArray() :array
{
$collection = [];
foreach ($this->values as $value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Vouchers/Exceptions/ImmutableData.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers\Exceptions;

Expand Down
2 changes: 1 addition & 1 deletion src/Vouchers/Exceptions/NoValidVouchers.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers\Exceptions;

Expand Down
2 changes: 1 addition & 1 deletion src/Vouchers/Exceptions/ValidationCallbackFail.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers\Exceptions;

Expand Down
2 changes: 1 addition & 1 deletion src/Vouchers/Exceptions/VoucherCodeInvalid.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers\Exceptions;

Expand Down
2 changes: 1 addition & 1 deletion src/Vouchers/Exceptions/VoucherValidationFailed.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers\Exceptions;

Expand Down
77 changes: 64 additions & 13 deletions src/Vouchers/Voucher.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
<?php declare(strict_types=1);

namespace Vouchers;

use Vouchers\Voucher\Code\GeneratorInterface;

class Voucher
{
/**
Expand Down Expand Up @@ -55,7 +57,7 @@ public function __construct(array $data = [], Voucher\Model $model = null)
*
* @return void
*/
private function parseData(array $data = [], Voucher\Model $model = null)
private function parseData(array $data = [], Voucher\Model $model = null) :void
{
// Validate Model
if ($model) {
Expand All @@ -73,7 +75,7 @@ private function parseData(array $data = [], Voucher\Model $model = null)
*
* @return void
*/
private function setArrayAsData(array $data)
private function setArrayAsData(array $data) :void
{
$this->data = array_merge($data, $this->data);
}
Expand All @@ -83,7 +85,7 @@ private function setArrayAsData(array $data)
*
* @return void
*/
private function generateVoucherCodeIfEmpty()
private function generateVoucherCodeIfEmpty() :void
{
$this->data[self::VOUCHER_CODE_KEY] = isset($this->data[self::VOUCHER_CODE_KEY]) ?
$this->data[self::VOUCHER_CODE_KEY] :
Expand All @@ -98,7 +100,7 @@ private function generateVoucherCodeIfEmpty()
*
* @return void
*/
private function processDataWithModel(array $data, Voucher\Model $model)
private function processDataWithModel(array $data, Voucher\Model $model) :void
{
$model->validate($data);
$this->data = array_merge($this->data, $data);
Expand All @@ -124,7 +126,7 @@ private function processDataWithModel(array $data, Voucher\Model $model)
*
* @return string
*/
public function generate()
public function generate() :string
{
$generator = new $this->generator();

Expand All @@ -138,7 +140,7 @@ public function generate()
*
* @return mixed
*/
public function get($key)
public function get($key) :?string
{
return array_key_exists($key, $this->data) ? $this->data[$key] : null;
}
Expand All @@ -151,7 +153,7 @@ public function get($key)
*
* @return mixed $value
*/
public function set($key, $value)
public function set($key, $value) :?string
{
if (in_array($key, $this->immutable)) {
throw new \Vouchers\Exceptions\ImmutableData();
Expand All @@ -165,9 +167,9 @@ public function set($key, $value)
/**
* Static function to get generator class.
*
* @return string
* @return GeneratorInterface
*/
public function getGenerator()
public function getGenerator() :GeneratorInterface
{
return $this->generator;
}
Expand All @@ -179,7 +181,7 @@ public function getGenerator()
*
* @return void
*/
public function addImmutableKey($key)
public function addImmutableKey($key) :void
{
array_push($this->immutable, $key);
}
Expand All @@ -189,7 +191,7 @@ public function addImmutableKey($key)
*
* @return string
*/
public function __toString()
public function __toString() :string
{
return $this->data[self::VOUCHER_CODE_KEY];
}
Expand All @@ -199,8 +201,57 @@ public function __toString()
*
* @return array
*/
public function toArray()
public function toArray() :array
{
return $this->data;
}

/**
* Add a magic call to get and set.
*
* @param string $method
* @param array $args
*
* @return mixed
*/
public function __call($method, $args)
{
# if we start with get
if (substr($method, 0, 3) == 'get') {
$key = lcfirst(substr($method, 3));

# split the key at its capital letters
$key = preg_split('/(?=[a-Z])/', $key);

# join the key with underscores
$key_string = "";
foreach($key as $k => $v) {
$key_string .= $v;
if($k < count($key) - 1) {
$key_string .= "_";
}
}

return $this->get($key_string);
}

# if we start with set
if (substr($method, 0, 3) == 'set') {
$key = lcfirst(substr($method, 3));

# split the key at its capital letters
$key = preg_split('/(?=[a-Z])/', $key);

# join the key with underscores
$key_string = "";
foreach ($key as $k => $v) {
$key_string .= $v;
if ($k < count($key) - 1) {
$key_string .= "_";
}
}

return $this->set($key_string, $args[0]);
}
}
}
Loading