Commit dddcc282 authored by bojanz's avatar bojanz

Issue #3091760 by bojanz: Add typehints to final classes

parent 3b4ef562
......@@ -51,7 +51,7 @@ final class Sequence {
* @return int
* The number.
*/
public function getNumber() {
public function getNumber() : int {
return $this->number;
}
......@@ -61,7 +61,7 @@ final class Sequence {
* @return int
* The generated timestamp.
*/
public function getGeneratedTime() {
public function getGeneratedTime() : int {
return $this->generated;
}
......@@ -71,7 +71,7 @@ final class Sequence {
* @return int
* The store ID, or 0 if the sequence is not store specific.
*/
public function getStoreId() {
public function getStoreId() : int {
return $this->storeId;
}
......
......@@ -109,7 +109,7 @@ final class Adjustment {
* @return string
* The adjustment type.
*/
public function getType() {
public function getType() : string {
return $this->type;
}
......@@ -119,7 +119,7 @@ final class Adjustment {
* @return string
* The adjustment label.
*/
public function getLabel() {
public function getLabel() : string {
return $this->label;
}
......@@ -129,7 +129,7 @@ final class Adjustment {
* @return \Drupal\commerce_price\Price
* The adjustment amount.
*/
public function getAmount() {
public function getAmount() : Price {
return $this->amount;
}
......@@ -139,7 +139,7 @@ final class Adjustment {
* @return bool
* TRUE if the adjustment is positive, FALSE otherwise.
*/
public function isPositive() {
public function isPositive() : bool {
return $this->amount->getNumber() >= 0;
}
......@@ -149,7 +149,7 @@ final class Adjustment {
* @return bool
* TRUE if the adjustment is negative, FALSE otherwise.
*/
public function isNegative() {
public function isNegative() : bool {
return $this->amount->getNumber() < 0;
}
......@@ -167,8 +167,8 @@ final class Adjustment {
/**
* Get the source identifier.
*
* @return string
* The source identifier.
* @return string|null
* The source identifier, if available.
*/
public function getSourceId() {
return $this->sourceId;
......@@ -180,7 +180,7 @@ final class Adjustment {
* @return bool
* TRUE if the adjustment is included in the base price, FALSE otherwise.
*/
public function isIncluded() {
public function isIncluded() : bool {
return $this->included;
}
......@@ -192,7 +192,7 @@ final class Adjustment {
* @return bool
* TRUE if the adjustment is locked, FALSE otherwise.
*/
public function isLocked() {
public function isLocked() : bool {
return $this->locked;
}
......@@ -202,7 +202,7 @@ final class Adjustment {
* @return array
* The array representation of the adjustment.
*/
public function toArray() {
public function toArray() : array {
return [
'type' => $this->type,
'label' => $this->label,
......@@ -223,7 +223,7 @@ final class Adjustment {
* @return static
* The resulting adjustment.
*/
public function add(Adjustment $adjustment) {
public function add(Adjustment $adjustment) : Adjustment {
$this->assertSameType($adjustment);
$this->assertSameSourceId($adjustment);
$definition = [
......@@ -242,7 +242,7 @@ final class Adjustment {
* @return static
* The resulting adjustment.
*/
public function subtract(Adjustment $adjustment) {
public function subtract(Adjustment $adjustment) : Adjustment {
$this->assertSameType($adjustment);
$this->assertSameSourceId($adjustment);
$definition = [
......@@ -261,7 +261,7 @@ final class Adjustment {
* @return static
* The resulting adjustment.
*/
public function multiply($number) {
public function multiply($number) : Adjustment {
$definition = [
'amount' => $this->amount->multiply($number),
] + $this->toArray();
......@@ -278,7 +278,7 @@ final class Adjustment {
* @return static
* The resulting adjustment.
*/
public function divide($number) {
public function divide($number) : Adjustment {
$definition = [
'amount' => $this->amount->divide($number),
] + $this->toArray();
......
......@@ -56,7 +56,7 @@ final class PriceCalculatorResult {
* @return \Drupal\commerce_price\Price
* The calculated price.
*/
public function getCalculatedPrice() {
public function getCalculatedPrice() : Price {
return $this->calculatedPrice;
}
......@@ -68,7 +68,7 @@ final class PriceCalculatorResult {
* @return \Drupal\commerce_price\Price
* The base price.
*/
public function getBasePrice() {
public function getBasePrice() : Price {
return $this->basePrice;
}
......@@ -78,7 +78,7 @@ final class PriceCalculatorResult {
* @return \Drupal\commerce_order\Adjustment[]
* The adjustments.
*/
public function getAdjustments() {
public function getAdjustments() : array {
return $this->adjustments;
}
......
......@@ -25,7 +25,7 @@ final class CreditCard {
* @return \Drupal\commerce_payment\CreditCardType
* The credit card type.
*/
public static function getType($id) {
public static function getType(string $id) : CreditCardType {
$types = self::getTypes();
if (!isset($types[$id])) {
throw new \InvalidArgumentException(sprintf('Invalid credit card type "%s"', $id));
......@@ -40,7 +40,7 @@ final class CreditCard {
* @return \Drupal\commerce_payment\CreditCardType[]
* The credit card types.
*/
public static function getTypes() {
public static function getTypes() : array {
$definitions = [
'visa' => [
'id' => 'visa',
......@@ -107,7 +107,7 @@ final class CreditCard {
* @return array
* The labels, keyed by ID.
*/
public static function getTypeLabels() {
public static function getTypeLabels() : array {
$types = self::getTypes();
$type_labels = array_map(function ($type) {
return $type->getLabel();
......@@ -153,7 +153,7 @@ final class CreditCard {
* @return bool
* TRUE if the credit card number matches the prefix, FALSE otherwise.
*/
public static function matchPrefix($number, $prefix) {
public static function matchPrefix(string $number, string $prefix) : bool {
if (is_numeric($prefix)) {
return substr($number, 0, strlen($prefix)) == $prefix;
}
......@@ -175,7 +175,7 @@ final class CreditCard {
* @return bool
* TRUE if the credit card number is valid, FALSE otherwise.
*/
public static function validateNumber($number, CreditCardType $type) {
public static function validateNumber(string $number, CreditCardType $type) : bool {
if (!is_numeric($number)) {
return FALSE;
}
......@@ -198,7 +198,7 @@ final class CreditCard {
* @return bool
* TRUE if the credit card number is valid, FALSE otherwise.
*/
public static function validateLuhn($number) {
public static function validateLuhn(string $number) : bool {
$total = 0;
foreach (array_reverse(str_split($number)) as $i => $digit) {
$digit = $i % 2 ? $digit * 2 : $digit;
......@@ -219,7 +219,7 @@ final class CreditCard {
* @return bool
* TRUE if the credit card expiration date is valid, FALSE otherwise.
*/
public static function validateExpirationDate($month, $year) {
public static function validateExpirationDate(string $month, string $year) : bool {
if ($month < 1 || $month > 12) {
return FALSE;
}
......@@ -244,7 +244,7 @@ final class CreditCard {
* @return int
* The expiration date as a unix timestamp.
*/
public static function calculateExpirationTimestamp($month, $year) {
public static function calculateExpirationTimestamp(string $month, string $year) : int {
// Credit cards expire on the last day of the month.
$month_start = strtotime($year . '-' . $month . '-01');
$last_day = date('t', $month_start);
......@@ -262,7 +262,7 @@ final class CreditCard {
* @return bool
* TRUE if the credit card security code is valid, FALSE otherwise.
*/
public static function validateSecurityCode($security_code, CreditCardType $type) {
public static function validateSecurityCode(string $security_code, CreditCardType $type) : bool {
if (!is_numeric($security_code)) {
return FALSE;
}
......
......@@ -82,7 +82,7 @@ final class CreditCardType {
* @return string
* The credit card type ID.
*/
public function getId() {
public function getId() : string {
return $this->id;
}
......@@ -92,7 +92,7 @@ final class CreditCardType {
* @return string
* The credit card type label.
*/
public function getLabel() {
public function getLabel() : string {
return $this->label;
}
......@@ -102,7 +102,7 @@ final class CreditCardType {
* @return array
* The credit card type number prefixes.
*/
public function getNumberPrefixes() {
public function getNumberPrefixes() : array {
return $this->numberPrefixes;
}
......@@ -112,7 +112,7 @@ final class CreditCardType {
* @return array
* The credit card type number lengths.
*/
public function getNumberLengths() {
public function getNumberLengths() : array {
return $this->numberLengths;
}
......@@ -122,7 +122,7 @@ final class CreditCardType {
* @return string
* The credit card type security code length.
*/
public function getSecurityCodeLength() {
public function getSecurityCodeLength() : string {
return $this->securityCodeLength;
}
......@@ -132,7 +132,7 @@ final class CreditCardType {
* @return bool
* TRUE if the credit card type uses Luhn validation, FALSE otherwise.
*/
public function usesLuhn() {
public function usesLuhn() : bool {
return $this->usesLuhn;
}
......
......@@ -74,7 +74,7 @@ final class PaymentOption {
* @return string
* The ID.
*/
public function getId() {
public function getId() : string {
return $this->id;
}
......@@ -84,7 +84,7 @@ final class PaymentOption {
* @return string
* The label.
*/
public function getLabel() {
public function getLabel() : string {
return $this->label;
}
......@@ -94,7 +94,7 @@ final class PaymentOption {
* @return string
* The payment gateway ID.
*/
public function getPaymentGatewayId() {
public function getPaymentGatewayId() : string {
return $this->paymentGatewayId;
}
......
......@@ -27,7 +27,7 @@ final class Calculator {
* @return string
* The result.
*/
public static function add($first_number, $second_number, $scale = 6) {
public static function add(string $first_number, string $second_number, int $scale = 6) : string {
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcadd($first_number, $second_number, $scale);
......@@ -48,7 +48,7 @@ final class Calculator {
* @return string
* The result.
*/
public static function subtract($first_number, $second_number, $scale = 6) {
public static function subtract(string $first_number, string $second_number, int $scale = 6) : string {
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcsub($first_number, $second_number, $scale);
......@@ -69,7 +69,7 @@ final class Calculator {
* @return string
* The result.
*/
public static function multiply($first_number, $second_number, $scale = 6) {
public static function multiply(string $first_number, string $second_number, int $scale = 6) : string {
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcmul($first_number, $second_number, $scale);
......@@ -90,7 +90,7 @@ final class Calculator {
* @return string
* The result.
*/
public static function divide($first_number, $second_number, $scale = 6) {
public static function divide(string $first_number, string $second_number, int $scale = 6) : string {
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
$result = bcdiv($first_number, $second_number, $scale);
......@@ -106,7 +106,7 @@ final class Calculator {
* @return string
* The result.
*/
public static function ceil($number) {
public static function ceil(string $number) : string {
if (self::compare($number, 0) == 1) {
$result = bcadd($number, '1', 0);
}
......@@ -125,7 +125,7 @@ final class Calculator {
* @return string
* The result.
*/
public static function floor($number) {
public static function floor(string $number) : string {
if (self::compare($number, 0) == 1) {
$result = bcadd($number, '0', 0);
}
......@@ -155,7 +155,7 @@ final class Calculator {
* @throws \InvalidArgumentException
* Thrown when an invalid (non-numeric or negative) precision is given.
*/
public static function round($number, $precision = 0, $mode = PHP_ROUND_HALF_UP) {
public static function round(string $number, int $precision = 0, int $mode = PHP_ROUND_HALF_UP) : string {
self::assertNumberFormat($number);
if (!is_numeric($precision) || $precision < 0) {
throw new \InvalidArgumentException('The provided precision should be a positive number');
......@@ -214,7 +214,7 @@ final class Calculator {
* @return int
* 0 if both numbers are equal, 1 if the first one is greater, -1 otherwise.
*/
public static function compare($first_number, $second_number, $scale = 6) {
public static function compare(string $first_number, string $second_number, int $scale = 6) : int {
self::assertNumberFormat($first_number);
self::assertNumberFormat($second_number);
return bccomp($first_number, $second_number, $scale);
......@@ -233,7 +233,7 @@ final class Calculator {
* @return string
* The trimmed number.
*/
public static function trim($number) {
public static function trim(string $number) : string {
if (strpos($number, '.') != FALSE) {
// The number is decimal, strip trailing zeroes.
// If no digits remain after the decimal point, strip it as well.
......
......@@ -33,7 +33,7 @@ final class Price {
* @param string $currency_code
* The currency code.
*/
public function __construct($number, $currency_code) {
public function __construct(string $number, string $currency_code) {
Calculator::assertNumberFormat($number);
$this->assertCurrencyCodeFormat($currency_code);
......@@ -49,7 +49,7 @@ final class Price {
*
* @return static
*/
public static function fromArray(array $price) {
public static function fromArray(array $price) : Price {
if (!isset($price['number'], $price['currency_code'])) {
throw new \InvalidArgumentException('Price::fromArray() called with a malformed array.');
}
......@@ -62,7 +62,7 @@ final class Price {
* @return string
* The number.
*/
public function getNumber() {
public function getNumber() : string {
return $this->number;
}
......@@ -72,7 +72,7 @@ final class Price {
* @return string
* The currency code.
*/
public function getCurrencyCode() {
public function getCurrencyCode() : string {
return $this->currencyCode;
}
......@@ -82,7 +82,7 @@ final class Price {
* @return string
* The string representation of the price.
*/
public function __toString() {
public function __toString() : string {
return Calculator::trim($this->number) . ' ' . $this->currencyCode;
}
......@@ -92,7 +92,7 @@ final class Price {
* @return array
* The array representation of the price.
*/
public function toArray() {
public function toArray() : array {
return ['number' => $this->number, 'currency_code' => $this->currencyCode];
}
......@@ -107,7 +107,7 @@ final class Price {
* @return static
* The resulting price.
*/
public function convert($currency_code, $rate = '1') {
public function convert($currency_code, $rate = '1') : Price {
$new_number = Calculator::multiply($this->number, $rate);
return new static($new_number, $currency_code);
}
......@@ -121,7 +121,7 @@ final class Price {
* @return static
* The resulting price.
*/
public function add(Price $price) {
public function add(Price $price) : Price {
$this->assertSameCurrency($this, $price);
$new_number = Calculator::add($this->number, $price->getNumber());
return new static($new_number, $this->currencyCode);
......@@ -136,7 +136,7 @@ final class Price {
* @return static
* The resulting price.
*/
public function subtract(Price $price) {
public function subtract(Price $price) : Price {
$this->assertSameCurrency($this, $price);
$new_number = Calculator::subtract($this->number, $price->getNumber());
return new static($new_number, $this->currencyCode);
......@@ -151,7 +151,7 @@ final class Price {
* @return static
* The resulting price.
*/
public function multiply($number) {
public function multiply(string $number) : Price {
$new_number = Calculator::multiply($this->number, $number);
return new static($new_number, $this->currencyCode);
}
......@@ -165,7 +165,7 @@ final class Price {
* @return static
* The resulting price.
*/
public function divide($number) {
public function divide(string $number) : Price {
$new_number = Calculator::divide($this->number, $number);
return new static($new_number, $this->currencyCode);
}
......@@ -179,7 +179,7 @@ final class Price {
* @return int
* 0 if both prices are equal, 1 if the first one is greater, -1 otherwise.
*/
public function compareTo(Price $price) {
public function compareTo(Price $price) : int {
$this->assertSameCurrency($this, $price);
return Calculator::compare($this->number, $price->getNumber());
}
......@@ -190,7 +190,7 @@ final class Price {
* @return bool
* TRUE if the price is positive, FALSE otherwise.
*/
public function isPositive() {
public function isPositive() : bool {
return Calculator::compare($this->number, '0') == 1;
}
......@@ -200,7 +200,7 @@ final class Price {
* @return bool
* TRUE if the price is negative, FALSE otherwise.
*/
public function isNegative() {
public function isNegative() : bool {
return Calculator::compare($this->number, '0') == -1;
}
......@@ -210,7 +210,7 @@ final class Price {
* @return bool
* TRUE if the price is zero, FALSE otherwise.
*/
public function isZero() {
public function isZero() : bool {
return Calculator::compare($this->number, '0') == 0;
}
......@@ -223,7 +223,7 @@ final class Price {
* @return bool
* TRUE if the prices are equal, FALSE otherwise.
*/
public function equals(Price $price) {
public function equals(Price $price) : bool {
return $this->compareTo($price) == 0;
}
......@@ -237,7 +237,7 @@ final class Price {
* TRUE if the current price is greater than the given price,
* FALSE otherwise.
*/
public function greaterThan(Price $price) {
public function greaterThan(Price $price) : bool {
return $this->compareTo($price) == 1;
}
......@@ -251,7 +251,7 @@ final class Price {
* TRUE if the current price is greater than or equal to the given price,
* FALSE otherwise.
*/
public function greaterThanOrEqual(Price $price) {
public function greaterThanOrEqual(Price $price) : bool {
return $this->greaterThan($price) || $this->equals($price);
}
......@@ -265,7 +265,7 @@ final class Price {
* TRUE if the current price is lesser than the given price,
* FALSE otherwise.
*/
public function lessThan(Price $price) {
public function lessThan(Price $price) : bool {
return $this->compareTo($price) == -1;
}
......@@ -279,7 +279,7 @@ final class Price {
* TRUE if the current price is lesser than or equal to the given price,
* FALSE otherwise.
*/
public function lessThanOrEqual(Price $price) {
public function lessThanOrEqual(Price $price) : bool {
return $this->lessThan($price) || $this->equals($price);
}
......
......@@ -73,7 +73,7 @@ final class PreparedAttribute {
* @return string
* The ID.
*/
public function getId() {
public function getId() : string {
return $this->id;
}
......@@ -83,7 +83,7 @@ final class PreparedAttribute {
* @return string
* The label.
*/
public function getLabel() {
public function getLabel() : string {
return $this->label;
}
......@@ -93,7 +93,7 @@ final class PreparedAttribute {
* @return string
* The element type.
*/
public function getElementType() {
public function getElementType() : string {
return $this->elementType;
}
......@@ -103,7 +103,7 @@ final class PreparedAttribute {
* @return bool
* TRUE if the attribute is required, FALSE otherwise.
*/
public function isRequired() {
public function isRequired() : bool {
return $this->required;
}
......@@ -113,7 +113,7 @@ final class PreparedAttribute {
* @return string[]
* The attribute values.
*/
public function getValues() {
public function getValues() : array {
return $this->values;
}
......@@ -123,7 +123,7 @@ final class PreparedAttribute {
* @return array
* The array representation of the prepared attribute.
*/
public function toArray() {
public function toArray() : array {
return [
'id' => $this->id,
'label' => $this->label,
......
......@@ -56,7 +56,7 @@ final class CouponCodePattern {
* @param int $length
* The length.
*/
public function __construct($type, $prefix = '', $suffix = '', $length = 8) {
public function __construct(string $type, string $prefix = '', string $suffix = '', int $length = 8) {
$pattern_types = [self::ALPHANUMERIC, self::ALPHABETIC, self::NUMERIC];
if (!in_array($type, $pattern_types)) {
throw new \InvalidArgumentException(sprintf('Unknown pattern type "%s".', $type));
......@@ -74,7 +74,7 @@ final class CouponCodePattern {
* @return string
* The pattern type.
*/
public function getType() {
public function getType() : string {
return $this->type;
}
......@@ -84,7 +84,7 @@ final class CouponCodePattern {
* @return string
* The prefix.
*/
public function getPrefix() {
public function getPrefix() : string {
return $this->prefix;
}
......@@ -94,7 +94,7 @@ final class CouponCodePattern {
* @return string
* The suffix.
*/
public function getSuffix() {
public function getSuffix() : string {
return $this->suffix;
}
......@@ -104,7 +104,7 @@ final class CouponCodePattern {
* @return int
* The length.
*/
public function getLength() {
public function getLength() : int {
return $this->length;
}
......
......@@ -47,7 +47,7 @@ final class VerificationResult {
* @param array $data
* The data. Optional.
*/
public function __construct($state, $timestamp, array $data = []) {
public function __construct(string $state, int $timestamp, array $data = []) {
$this->state = $state;
$this->timestamp = $timestamp;
$this->data = $data;
......@@ -63,7 +63,7 @@ final class VerificationResult {
*
* @return static
*/
public static function success($timestamp, array $data = []) {
public static function success(int $timestamp, array $data = []) : VerificationResult {
return new static(self::STATE_SUCCESS, $timestamp, $data);
}
......@@ -77,7 +77,7 @@ final class VerificationResult {
*
* @return static
*/
public static function failure($timestamp, array $data = []) {
public static function failure(int $timestamp, array $data = []) : VerificationResult {
return new static(self::STATE_FAILURE, $timestamp, $data);
}
......@@ -91,7 +91,7 @@ final class VerificationResult {
*
* @return static
*/
public static function unknown($timestamp, array $data = []) {
public static function unknown(int $timestamp, array $data = []) : VerificationResult {
return new static(self::STATE_UNKNOWN, $timestamp, $data);
}
......@@ -101,7 +101,7 @@ final class VerificationResult {
* @return bool
* TRUE if the verification was successful, FALSE otherwise.
*/
public function isSuccess() {
public function isSuccess() : bool {
return $this->state == self::STATE_SUCCESS;
}
......@@ -111,7 +111,7 @@ final class VerificationResult {
* @return bool
* TRUE if the verification failed, FALSE otherwise.
*/
public function isFailure() {
public function isFailure() : bool {
return $this->state == self::STATE_FAILURE;
}
......@@ -123,7 +123,7 @@ final class VerificationResult {
* @return bool
* TRUE if the verification state is unknown, FALSE otherwise.
*/
public function isUnknown() {
public function isUnknown() : bool {
return $this->state == self::STATE_UNKNOWN;
}
......@@ -133,7 +133,7 @@ final class VerificationResult {
* @return string
* The state. One of the STATE_ constants.
*/
public function getState() {
public function getState() : string {
return $this->state;