Coder Social home page Coder Social logo

wp-money's People

Contributors

knit-pay avatar remcotolsma avatar rvdsteege avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

knit-pay

wp-money's Issues

Logic `Money->format( $format = null )` and `Money->format_i18n( $format = null )` confusing?

In both functions the $format parameter is optional, but the parameter doesn't work the same way. If null is passed in Money->format_i18n( $format = null ) function it will fall back to Money::get_default_format():

wp-money/src/Money.php

Lines 71 to 73 in 86b41ad

if ( is_null( $format ) ) {
$format = self::get_default_format();
}

If null is passed in Money->format( $format = null ) function it will fall back to '%2$s':

wp-money/src/Money.php

Lines 127 to 129 in 86b41ad

if ( is_null( $format ) ) {
$format = '%2$s';
}

Currently it's very imported that Money->format( $format = null ) falls back to '%2$s', otherwise some of the gateways will break:

pronamic/wp-pronamic-pay-mollie@08bc30c

However, it is not very logical that the Money->format( $format = null ) does not process currency information by default, without currency information it's just a number.

We could make this more clear like this: Money->format_amount( $number_decimals = null );, if $number_decimals is null we can fall back to the currency $number_decimals value, similarly an i18n version: $money->format_i18n_amount( $number_decimals );.

Naming can also be different:

  • Money->number_format( $number_decimals = null )
  • Money->amount_format( $number_decimals = null )
  • Money->number_format_i18n( $number_decimals = null )
  • Money->amount_format_i18n( $number_decimals = null )

I think i prefer number_format and number_format_i18n:

The default format for Money->format( $format = null ) can then be changed to '%3$s%2$s' so that it contains at least the currency code by default.

Maybe we should also consider replacing the printf directives with placeholders:

$placeholders = array(
	'{non_breaking_space}'            => ...,
	'{currency_symbol}'               => ...,
	'{currency_alphabetic_code}'      => ...,
	'{currency_numeric_code}'         => ...,
	'{currency_name}'                 => ...,
	'{amount}'                        => ...,
	'{amount_i18n}'                   => ...,
	'{amount_no_trailing_zeros}'      => ...,
	'{amount_i18n_no_trailing_zeros}' => ...,
);

Current use is not very clear:

echo $money->format( '%3$s%2$s' );

You have no idea what the result is, following is more clear:

echo $money->format( '{currency_alphabetic_code}{non_breaking_space}{amount}' );

@rvdsteege thoughts?

Tax information removed in taxed money calculations

When performing calculations in a TaxedMoney object, the tax details are removed.

$taxed = new TaxedMoney( 10, 'EUR', 2.10 );

var_dump( $taxed->get_tax_value() ); // string '2.1'

var_dump( $taxed->multiply( 5 )->get_tax_value() ); // null

This is caused by the fact that we're instantiating a new static (either Money or TaxedMoney) object from e.g. the multiply() method.

wp-money/src/Money.php

Lines 328 to 344 in 6b144b3

/**
* Returns a new Money object that represents
* the multiplied value of this Money object.
*
* @link https://github.com/moneyphp/money/blob/v3.2.1/src/Money.php#L299-L316
*
* @param mixed $multiplier Multiplier.
*
* @return Money
*/
public function multiply( $multiplier ) {
$multiplier = Number::from_mixed( $multiplier );
$result = $this->amount->multiply( $multiplier );
return new static( $result, $this->currency );
}

Also, PHPStan currently warns that the method signature of TaxedMoney::__construct() is not enforced to be equal to the Money constructor signature:

Unsafe usage of new static().
๐Ÿ’ก See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static

Both issues can be resolved by introducing a MoneyInterface class, which needs to be implemented by both the Money and TaxedMoney classes. The interface should contain abstract methods for the constructor and calculation methods.

The multiply() method is currently in use in Gravity Forms subscription period alignment, where losing tax details is fine as we're not specifying taxes for the payment lines in this integration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.