Skip to content

Commit

Permalink
Update to php 8 (#15)
Browse files Browse the repository at this point in the history
* Cleanup interface

* Fix readme

* Added quantity test
  • Loading branch information
cjonstrup authored May 25, 2022
1 parent 1f96861 commit 6724e49
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1']
php: ['8.0', '8.1']
dependency-version: [ prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
Expand Down
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ would like your basket data to be stored elsewhere.
Using [composer](https://packagist.org/packages/lenius/basket):

```bash
$ composer require lenius/basket:^3.0
$ composer require lenius/basket:^4.0 (PHP7.4)
$ composer require lenius/basket:^5.0 (PHP8.x)
```

## Usage
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4|^8.0"
"php": "^8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "3.4.0",
"friendsofphp/php-cs-fixer": "^3.4",
"php-coveralls/php-coveralls": "2.5.2",
"phpstan/phpstan": "0.12.99",
"phpunit/phpunit": "^8.0|^9.0",
"phpstan/phpstan": "^1.7",
"phpunit/phpunit": "^9.0",
"squizlabs/php_codesniffer": "3.6.2",
"vimeo/psalm": "4.15.0"
"vimeo/psalm": "^4.3"
},
"config": {
"sort-packages": true
Expand All @@ -39,7 +39,8 @@
"scripts": {
"psalm": "vendor/bin/psalm --show-info=true",
"fix": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"test": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover build/logs/clover.xml --coverage-html build/logs/phpunit-html/",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover build/logs/clover.xml --coverage-html build/logs/phpunit-html/",
"test": "vendor/bin/phpunit",
"stan": "vendor/bin/phpstan analyse"
}
}
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ parameters:
reportUnmatchedIgnoredErrors: false
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
excludePaths:
- './vendor'
66 changes: 34 additions & 32 deletions src/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* This file is part of Lenius Basket, a PHP package to handle
* your shopping basket.
*
* Copyright (c) 2017 Lenius.
* Copyright (c) 2022 Lenius.
* https://github.com/lenius/basket
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Carsten Jonstrup<[email protected]>
* @copyright 2017 Lenius.
* @copyright 2022 Lenius.
*
* @version production
*
Expand Down Expand Up @@ -48,8 +48,8 @@ class Basket
/**
* Basket constructor.
*
* @param StorageInterface $store The interface for storing the cart data
* @param IdentifierInterface $identifier The interface for storing the identifier
* @param StorageInterface $store
* @param IdentifierInterface $identifier
*/
public function __construct(StorageInterface $store, IdentifierInterface $identifier)
{
Expand All @@ -71,9 +71,9 @@ public function __construct(StorageInterface $store, IdentifierInterface $identi
*
* @param bool $asArray
*
* @return array An array of Item objects
* @return array
*/
public function &contents($asArray = false): array
public function &contents(bool $asArray = false): array
{
return $this->store->data($asArray);
}
Expand All @@ -82,7 +82,8 @@ public function &contents($asArray = false): array
* Insert an item into the basket.
*
* @param ItemInterface $item
* @return string A unique item identifier
*
* @return string
*/
public function insert(ItemInterface $item): string
{
Expand All @@ -91,7 +92,7 @@ public function insert(ItemInterface $item): string
$itemIdentifier = $this->createItemIdentifier($item);

if ($this->has($itemIdentifier) && $this->item($itemIdentifier) instanceof ItemInterface) {
$item->quantity = $this->item($itemIdentifier)->quantity + $item->quantity;
$item->setQuantity($this->item($itemIdentifier)->getQuantity() + $item->getQuantity());
$this->update($itemIdentifier, $item);

return $itemIdentifier;
Expand All @@ -107,13 +108,13 @@ public function insert(ItemInterface $item): string
/**
* Update an item.
*
* @param string $itemIdentifier The unique item identifier
* @param mixed $key The key to update, or an array of key-value pairs
* @param mixed $value The value to set $key to
* @param string $itemIdentifier
* @param mixed $key
* @param mixed $value
*/
public function update(string $itemIdentifier, $key, $value = null): void
{
/** @var Item $item */
/** @var ItemInterface $item */
foreach ($this->contents() as $item) {
if ($item->identifier == $itemIdentifier) {
$item->update($key, $value);
Expand All @@ -126,7 +127,7 @@ public function update(string $itemIdentifier, $key, $value = null): void
/**
* Remove an item from the basket.
*
* @param string $identifier Unique item identifier
* @param string $identifier
*/
public function remove(string $identifier): void
{
Expand All @@ -144,9 +145,9 @@ public function destroy(): void
/**
* Check if the basket has a specific item.
*
* @param string $itemIdentifier The unique item identifier
* @param string $itemIdentifier
*
* @return bool Yes or no?
* @return bool
*/
public function has(string $itemIdentifier): bool
{
Expand All @@ -156,31 +157,31 @@ public function has(string $itemIdentifier): bool
/**
* Return a specific item object by identifier.
*
* @param string $itemIdentifier The unique item identifier
* @param string $itemIdentifier
*
* @return Item|bool
* @return ItemInterface|bool
*/
public function item(string $itemIdentifier)
public function item(string $itemIdentifier): ItemInterface|bool
{
return $this->store->item($itemIdentifier);
}

/**
* Returns the first occurance of an item with a given id.
* Returns the first occurrence of an item with a given id.
*
* @param string $id The item id
* @param string $id
*
* @return bool|Item
* @return bool|ItemInterface
*/
public function find(string $id)
public function find(string $id): ItemInterface|bool
{
return $this->store->find($id);
}

/**
* The total tax value for the basket.
*
* @return float The total tax value
* @return float
*/
public function tax(): float
{
Expand All @@ -197,7 +198,7 @@ public function tax(): float
/**
* The total weight value for the basket.
*
* @return float The total weight value
* @return float
*/
public function weight(): float
{
Expand All @@ -214,11 +215,11 @@ public function weight(): float
/**
* The total value of the basket.
*
* @param bool $includeTax Include tax on the total?
* @param bool $includeTax
*
* @return float The total basket value
* @return float
*/
public function total($includeTax = true): float
public function total(bool $includeTax = true): float
{
$total = 0;

Expand All @@ -233,17 +234,17 @@ public function total($includeTax = true): float
/**
* The total number of items in the basket.
*
* @param bool $unique Just return unique items?
* @param bool $unique
*
* @return int Total number of items
* @return int
*/
public function totalItems($unique = false): int
public function totalItems(bool $unique = false): int
{
$total = 0;

/** @var Item $item */
foreach ($this->contents() as $item) {
$total += $unique ? 1 : $item->quantity;
$total += $unique ? 1 : $item->getQuantity();
}

return $total;
Expand All @@ -265,7 +266,8 @@ public function setIdentifier(string $identifier): void
* Create a unique item identifier.
*
* @param ItemInterface $item
* @return string An md5 hash of item
*
* @return string
*/
protected function createItemIdentifier(ItemInterface $item): string
{
Expand Down
6 changes: 3 additions & 3 deletions src/IdentifierInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* This file is part of Lenius Basket, a PHP package to handle
* your shopping basket.
*
* Copyright (c) 2017 Lenius.
* Copyright (c) 2022 Lenius.
* https://github.com/lenius/basket
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Carsten Jonstrup<[email protected]>
* @copyright 2017 Lenius.
* @copyright 2022 Lenius.
*
* @version production
*
Expand All @@ -35,7 +35,7 @@ public function get(): string;
/**
* Regenerate the identifier.
*
* @return string The identifier
* @return string
*/
public function regenerate(): string;

Expand Down
Loading

0 comments on commit 6724e49

Please sign in to comment.