mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-23 23:51:25 +00:00
StyleCI
This commit is contained in:
@@ -2,9 +2,8 @@
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Gloudemans\Shoppingcart\CartItemOptions;
|
||||
use Money\Money;
|
||||
use Money\Currency;
|
||||
use Money\Money;
|
||||
|
||||
trait CanBeBought
|
||||
{
|
||||
@@ -25,9 +24,9 @@ trait CanBeBought
|
||||
{
|
||||
if (($name = $this->getAttribute('name'))) {
|
||||
return $name;
|
||||
} else if (($title = $this->getAttribute('title'))) {
|
||||
} elseif (($title = $this->getAttribute('title'))) {
|
||||
return $title;
|
||||
} else if (($description = $this->getAttribute('description'))) {
|
||||
} elseif (($description = $this->getAttribute('description'))) {
|
||||
return $description;
|
||||
} else {
|
||||
return null;
|
||||
|
||||
34
src/Cart.php
34
src/Cart.php
@@ -2,11 +2,8 @@
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Closure;
|
||||
use InvalidArgumentException;
|
||||
use Carbon\Carbon;
|
||||
use Money\Money;
|
||||
use Money\Currency;
|
||||
use Closure;
|
||||
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
||||
use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier;
|
||||
use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException;
|
||||
@@ -17,8 +14,11 @@ use Illuminate\Database\DatabaseManager;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Session\SessionManager;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use InvalidArgumentException;
|
||||
use Money\Currency;
|
||||
use Money\Money;
|
||||
|
||||
class Cart
|
||||
{
|
||||
@@ -116,23 +116,23 @@ class Cart
|
||||
{
|
||||
/* Allow adding a CartItem by raw parameters */
|
||||
if (is_int($id) || is_string($id)) {
|
||||
if (! is_null($nameOrQty) && ! is_string($nameOrQty)) {
|
||||
if (!is_null($nameOrQty) && !is_string($nameOrQty)) {
|
||||
throw new InvalidArgumentException('$nameOrQty must be of type string (name) or null when adding with raw parameters');
|
||||
}
|
||||
|
||||
if (! is_null($qtyOrOptions) && ! is_int($qtyOrOptions)) {
|
||||
if (!is_null($qtyOrOptions) && !is_int($qtyOrOptions)) {
|
||||
throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) or null when adding with raw parameters');
|
||||
}
|
||||
|
||||
return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight ?: 0, $options ?: new CartItemOptions([])));
|
||||
}
|
||||
/* Also allow passing a Buyable instance, get data from the instance rather than parameters */
|
||||
else if ($id instanceof Buyable) {
|
||||
if (! is_null($qtyOrOptions) && ! is_int($nameOrQty)) {
|
||||
elseif ($id instanceof Buyable) {
|
||||
if (!is_null($qtyOrOptions) && !is_int($nameOrQty)) {
|
||||
throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) when adding a Buyable instance');
|
||||
}
|
||||
|
||||
if (! is_null($qtyOrOptions) && ! $qtyOrOptions instanceof CartItemOptions) {
|
||||
if (!is_null($qtyOrOptions) && !$qtyOrOptions instanceof CartItemOptions) {
|
||||
throw new InvalidArgumentException('$qtyOrOptions must be of type CartItemOptions (options) or null when adding a Buyable instance');
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ class Cart
|
||||
return $this->addCartItem($cartItem);
|
||||
}
|
||||
/* Also allow passing multiple definitions at the same time, simply call same method and collec return value */
|
||||
else if (is_array($id)) {
|
||||
elseif (is_array($id)) {
|
||||
/* Check if this iterable contains instances */
|
||||
if (is_array(head($id)) || head($id) instanceof Buyable) {
|
||||
return array_map(function (Buyable|iterable $item) {
|
||||
@@ -179,7 +179,7 @@ class Cart
|
||||
{
|
||||
$item->setInstance($this->currentInstance());
|
||||
|
||||
if (! $keepDiscount) {
|
||||
if (!$keepDiscount) {
|
||||
$item->setDiscount($this->discount);
|
||||
}
|
||||
|
||||
@@ -363,7 +363,7 @@ class Cart
|
||||
if ($calculated instanceof Money) {
|
||||
return $calculated;
|
||||
} else {
|
||||
throw new \TypeError("Calculated price is not an instance of Money");
|
||||
throw new \TypeError('Calculated price is not an instance of Money');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,7 +381,7 @@ class Cart
|
||||
if ($calculated instanceof Money) {
|
||||
return $calculated;
|
||||
} else {
|
||||
throw new \TypeError("Calculated discount is not an instance of Money");
|
||||
throw new \TypeError('Calculated discount is not an instance of Money');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -397,7 +397,7 @@ class Cart
|
||||
if ($calculated instanceof Money) {
|
||||
return $calculated;
|
||||
} else {
|
||||
throw new \TypeError("Calculated subtotal is not an instance of Money");
|
||||
throw new \TypeError('Calculated subtotal is not an instance of Money');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -427,7 +427,7 @@ class Cart
|
||||
if ($calculated instanceof Money) {
|
||||
return $calculated;
|
||||
} else {
|
||||
throw new \TypeError("Calculated total is not an instance of Money");
|
||||
throw new \TypeError('Calculated total is not an instance of Money');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -443,7 +443,7 @@ class Cart
|
||||
if (is_int($calculated)) {
|
||||
return $calculated;
|
||||
} else {
|
||||
throw new \TypeError("Calculated weight was not an integer");
|
||||
throw new \TypeError('Calculated weight was not an integer');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,9 @@ use Illuminate\Contracts\Support\Jsonable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Money\Money;
|
||||
use Money\Formatter\DecimalMoneyFormatter;
|
||||
use Money\Currencies\ISOCurrencies;
|
||||
use Money\Formatter\DecimalMoneyFormatter;
|
||||
use Money\Money;
|
||||
|
||||
class CartItem implements Arrayable, Jsonable
|
||||
{
|
||||
@@ -118,7 +118,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
*
|
||||
* @param mixed $model
|
||||
*/
|
||||
public function associate(string|Model $model) : self
|
||||
public function associate(string|Model $model): self
|
||||
{
|
||||
$this->associatedModel = is_string($model) ? $model : get_class($model);
|
||||
|
||||
@@ -128,7 +128,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
/**
|
||||
* Set the tax rate.
|
||||
*/
|
||||
public function setTaxRate(float $taxRate) : self
|
||||
public function setTaxRate(float $taxRate): self
|
||||
{
|
||||
$this->taxRate = $taxRate;
|
||||
|
||||
@@ -138,7 +138,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
/**
|
||||
* Set the discount rate.
|
||||
*/
|
||||
public function setDiscount(float|Money $discount) : self
|
||||
public function setDiscount(float|Money $discount): self
|
||||
{
|
||||
$this->discount = $discount;
|
||||
|
||||
@@ -148,7 +148,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
/**
|
||||
* Set cart instance.
|
||||
*/
|
||||
public function setInstance(?string $instance) : self
|
||||
public function setInstance(?string $instance): self
|
||||
{
|
||||
$this->instance = $instance;
|
||||
|
||||
@@ -197,7 +197,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the tax, based on the subtotal (all previous calculations) and set tax rate
|
||||
* This is the tax, based on the subtotal (all previous calculations) and set tax rate.
|
||||
*/
|
||||
public function tax(): Money
|
||||
{
|
||||
@@ -223,18 +223,20 @@ class CartItem implements Arrayable, Jsonable
|
||||
/**
|
||||
* Create a new instance from a Buyable.
|
||||
*/
|
||||
public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null) : self
|
||||
public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null): self
|
||||
{
|
||||
$options = $options ?: new CartItemOptions([]);
|
||||
|
||||
return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $qty, $item->getBuyableWeight($options), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance from the given array.
|
||||
*/
|
||||
public static function fromArray(array $attributes) : self
|
||||
public static function fromArray(array $attributes): self
|
||||
{
|
||||
$options = new CartItemOptions(Arr::get($attributes, 'options', []));
|
||||
|
||||
return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['qty'], $attributes['weight'], $options);
|
||||
}
|
||||
|
||||
@@ -243,9 +245,10 @@ class CartItem implements Arrayable, Jsonable
|
||||
*
|
||||
* @param int|string $id
|
||||
*/
|
||||
public static function fromAttributes(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) : self
|
||||
public static function fromAttributes(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null): self
|
||||
{
|
||||
$options = $options ?: new CartItemOptions([]);
|
||||
|
||||
return new self($id, $name, $price, $qty, $weight, $options);
|
||||
}
|
||||
|
||||
@@ -288,7 +291,7 @@ class CartItem implements Arrayable, Jsonable
|
||||
/**
|
||||
* Generate a unique id for the cart item.
|
||||
*/
|
||||
private static function formatMoney(Money $money) : string
|
||||
private static function formatMoney(Money $money): string
|
||||
{
|
||||
return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($money);
|
||||
}
|
||||
@@ -296,10 +299,10 @@ class CartItem implements Arrayable, Jsonable
|
||||
/**
|
||||
* Generate a unique id for the cart item.
|
||||
*/
|
||||
protected function generateRowId(string $id, array $options) : string
|
||||
protected function generateRowId(string $id, array $options): string
|
||||
{
|
||||
ksort($options);
|
||||
|
||||
return md5($id . serialize($options));
|
||||
return md5($id.serialize($options));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Illuminate\Auth\Events\Logout;
|
||||
use Illuminate\Session\SessionManager;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ShoppingcartServiceProvider extends ServiceProvider
|
||||
|
||||
@@ -5,9 +5,9 @@ namespace Gloudemans\Tests\Shoppingcart;
|
||||
use Gloudemans\Shoppingcart\CartItem;
|
||||
use Gloudemans\Shoppingcart\CartItemOptions;
|
||||
use Gloudemans\Shoppingcart\ShoppingcartServiceProvider;
|
||||
use Orchestra\Testbench\TestCase;
|
||||
use Money\Money;
|
||||
use Money\Currency;
|
||||
use Money\Money;
|
||||
use Orchestra\Testbench\TestCase;
|
||||
|
||||
class CartItemTest extends TestCase
|
||||
{
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
namespace Gloudemans\Tests\Shoppingcart;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Money\Money;
|
||||
use Money\Currency;
|
||||
use Gloudemans\Shoppingcart\Cart;
|
||||
use Gloudemans\Shoppingcart\CartItem;
|
||||
use Gloudemans\Shoppingcart\CartItemOptions;
|
||||
@@ -15,6 +13,8 @@ use Gloudemans\Tests\Shoppingcart\Fixtures\Identifiable;
|
||||
use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Money\Currency;
|
||||
use Money\Money;
|
||||
use Orchestra\Testbench\TestCase;
|
||||
|
||||
class CartTest extends TestCase
|
||||
@@ -31,7 +31,7 @@ class CartTest extends TestCase
|
||||
protected function getPackageProviders($app)
|
||||
{
|
||||
return [
|
||||
ShoppingcartServiceProvider::class
|
||||
ShoppingcartServiceProvider::class,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
namespace Gloudemans\Tests\Shoppingcart\Fixtures;
|
||||
|
||||
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
||||
use Gloudemans\Shoppingcart\CartItemOptions;
|
||||
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Money\Money;
|
||||
use Money\Currency;
|
||||
use Money\Money;
|
||||
|
||||
class BuyableProduct extends Model implements Buyable
|
||||
{
|
||||
@@ -48,7 +48,7 @@ class BuyableProduct extends Model implements Buyable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBuyableDescription(CartItemOptions $options) : ?string
|
||||
public function getBuyableDescription(CartItemOptions $options): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ class ProductModel extends Model
|
||||
{
|
||||
public $someValue = 'Some value';
|
||||
|
||||
public function find($id) : self
|
||||
public function find($id): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user