diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 9171a9a2..6774ea00 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -3,7 +3,10 @@ // This is based on the `@Symfony` rule set documented in `vendor/friendsofphp/php-cs-fixer/doc/ruleSets/Symfony.rst`. $finder = PhpCsFixer\Finder::create() - ->in(__DIR__ . '/lib'); + ->in([ + __DIR__ . '/lib', + __DIR__ . '/Tests', + ]); return (new PhpCsFixer\Config()) ->setRules([ diff --git a/Tests/Unit/FinTsTestCase.php b/Tests/Unit/FinTsTestCase.php index 9cf4fa1d..cf377c12 100644 --- a/Tests/Unit/FinTsTestCase.php +++ b/Tests/Unit/FinTsTestCase.php @@ -103,9 +103,8 @@ protected function setUpConnection() + strlen($mockMiddle) + strlen($hnvsd) + strlen($mockSuffix); $newLength = str_pad($newLength, HNHBKv3::NACHRICHTENGROESSE_LENGTH, '0', STR_PAD_LEFT); return $mockPrefix . $newLength . $mockMiddle . $hnvsd . $mockSuffix; - } else { - return $mockResponse; } + return $mockResponse; }); return $this->connection; } diff --git a/lib/Fhp/FinTs.php b/lib/Fhp/FinTs.php index 24f1b6bf..a509328d 100644 --- a/lib/Fhp/FinTs.php +++ b/lib/Fhp/FinTs.php @@ -346,7 +346,7 @@ public function execute(BaseAction $action): void $message = MessageBuilder::create()->add($requestSegments); // Add HKTAN for authentication if necessary. - if (!($this->getSelectedTanMode() instanceof NoPsd2TanMode)) { + if (!$this->getSelectedTanMode() instanceof NoPsd2TanMode) { if (($needTanForSegment = $action->getNeedTanForSegment()) !== null) { $message->add(HKTANFactory::createProzessvariante2Step1( $this->requireTanMode(), $this->selectedTanMedium, $needTanForSegment)); @@ -421,11 +421,10 @@ private function processServerResponse(BaseAction $action, Message $response, ?H if ($confirmationRequest->getVerificationResult() === VopVerificationResult::CompletedFullMatch) { // If someone hits this branch in practice, we can implement it. throw new UnsupportedException('Combined VOP match confirmation and TAN request'); - } else { - throw new UnexpectedResponseException( - 'Unexpected TAN request on VOP result: ' . $confirmationRequest->getVerificationResult() - ); } + throw new UnexpectedResponseException( + 'Unexpected TAN request on VOP result: ' . $confirmationRequest->getVerificationResult() + ); } } } @@ -684,7 +683,7 @@ public function pollAction(BaseAction $action): void public function confirmVop(BaseAction $action): void { $vopConfirmationRequest = $action->getVopConfirmationRequest(); - if (!($vopConfirmationRequest instanceof VopConfirmationRequestImpl)) { + if (!$vopConfirmationRequest instanceof VopConfirmationRequestImpl) { throw new \InvalidArgumentException('Unexpected type: ' . gettype($vopConfirmationRequest)); } // We need to send the original request again, plus HKVPA as the confirmation. @@ -697,7 +696,7 @@ public function confirmVop(BaseAction $action): void ->add(VopHelper::createHKVPAForConfirmation($vopConfirmationRequest)); // Add HKTAN for authentication if necessary. - if (!($this->getSelectedTanMode() instanceof NoPsd2TanMode)) { + if (!$this->getSelectedTanMode() instanceof NoPsd2TanMode) { if (($needTanForSegment = $action->getNeedTanForSegment()) !== null) { $message->add(HKTANFactory::createProzessvariante2Step1( $this->requireTanMode(), $this->selectedTanMedium, $needTanForSegment)); @@ -930,7 +929,7 @@ private function ensureSynchronized(): void // banks that don't support PSD2, we just keep the dummy TAN mode, as they wouldn't even understand 999. $oldTanMode = $this->selectedTanMode; $oldTanMedium = $this->selectedTanMedium; - if (!($this->selectedTanMode instanceof NoPsd2TanMode)) { + if (!$this->selectedTanMode instanceof NoPsd2TanMode) { $this->selectedTanMode = null; } $this->selectedTanMedium = null; diff --git a/lib/Fhp/Model/StatementOfAccount/Transaction.php b/lib/Fhp/Model/StatementOfAccount/Transaction.php index af3151fb..7ebb9d7c 100644 --- a/lib/Fhp/Model/StatementOfAccount/Transaction.php +++ b/lib/Fhp/Model/StatementOfAccount/Transaction.php @@ -254,9 +254,8 @@ public function getMainDescription(): string { if (array_key_exists('SVWZ', $this->structuredDescription)) { return $this->structuredDescription['SVWZ']; - } else { - return ''; } + return ''; } /** @@ -266,9 +265,8 @@ public function getEndToEndID(): string { if (array_key_exists('EREF', $this->structuredDescription)) { return $this->structuredDescription['EREF']; - } else { - return ''; } + return ''; } /** diff --git a/lib/Fhp/Protocol/BPD.php b/lib/Fhp/Protocol/BPD.php index 5241ab46..fc18f5a3 100644 --- a/lib/Fhp/Protocol/BPD.php +++ b/lib/Fhp/Protocol/BPD.php @@ -85,7 +85,7 @@ public function getBankName() public function getAllSupportedParameters(string $type): array { return array_filter($this->parameters[$type] ?? [], function (BaseSegment $segment) { - return !($segment instanceof AnonymousSegment); + return !$segment instanceof AnonymousSegment; }); } @@ -101,7 +101,7 @@ public function getLatestSupportedParameters(string $type): ?BaseSegment return null; } foreach ($this->parameters[$type] as $segment) { - if (!($segment instanceof AnonymousSegment)) { + if (!$segment instanceof AnonymousSegment) { return $segment; } } diff --git a/lib/Fhp/Protocol/Message.php b/lib/Fhp/Protocol/Message.php index b4babcfc..853f64dc 100644 --- a/lib/Fhp/Protocol/Message.php +++ b/lib/Fhp/Protocol/Message.php @@ -320,11 +320,11 @@ public static function parse(string $rawMessage): Message // Message header and footer must always be there, or something went badly wrong. $result->header = $segments[0]; $result->footer = $segments[count($segments) - 1]; - if (!($result->header instanceof HNHBKv3)) { + if (!$result->header instanceof HNHBKv3) { $actual = $result->header->getName(); throw new \InvalidArgumentException("Expected first segment to be HNHBK, but got $actual: $rawMessage"); } - if (!($result->footer instanceof HNHBSv1)) { + if (!$result->footer instanceof HNHBSv1) { $actual = $result->footer->getName(); throw new \InvalidArgumentException("Expected last segment to be HNHBS, but got $actual: $rawMessage"); } @@ -332,7 +332,7 @@ public static function parse(string $rawMessage): Message // Check if there's an encryption header and "encrypted" data. // Section B.8 specifies that there are exactly 4 segments: HNHBK, HNVSK, HNVSD, HNHBS. if (count($segments) === 4 && $segments[1] instanceof HNVSKv3) { - if (!($segments[2] instanceof HNVSDv1)) { + if (!$segments[2] instanceof HNVSDv1) { throw new \InvalidArgumentException("Expected third segment to be HNVSD: $rawMessage"); } $result->wrapperSegments = $segments; diff --git a/lib/Fhp/Segment/BaseSegment.php b/lib/Fhp/Segment/BaseSegment.php index 3ce4718f..843e6a4d 100644 --- a/lib/Fhp/Segment/BaseSegment.php +++ b/lib/Fhp/Segment/BaseSegment.php @@ -123,10 +123,9 @@ public static function parse(string $rawSegment): static if (static::class === BaseSegment::class) { // Called as BaseSegment::parse(), so we need to determine the right segment type/class. return Parser::detectAndParseSegment($rawSegment); - } else { - // The parse() function was called on the segment subclass itself. - return Parser::parseSegment($rawSegment, static::class); } + // The parse() function was called on the segment subclass itself. + return Parser::parseSegment($rawSegment, static::class); } /** diff --git a/lib/Fhp/Segment/Common/Sdo.php b/lib/Fhp/Segment/Common/Sdo.php index c43cbbda..2b70587c 100644 --- a/lib/Fhp/Segment/Common/Sdo.php +++ b/lib/Fhp/Segment/Common/Sdo.php @@ -36,11 +36,13 @@ public function getAmount(): float { if ($this->sollHabenKennzeichen === self::CREDIT) { return $this->betrag->wert; - } elseif ($this->sollHabenKennzeichen === self::DEBIT) { + } + + if ($this->sollHabenKennzeichen === self::DEBIT) { return -1 * $this->betrag->wert; - } else { - throw new \InvalidArgumentException("Invalid sollHabenKennzeichen: $this->sollHabenKennzeichen"); } + + throw new \InvalidArgumentException("Invalid sollHabenKennzeichen: $this->sollHabenKennzeichen"); } public function getCurrency(): string diff --git a/lib/Fhp/Segment/SegmentDescriptor.php b/lib/Fhp/Segment/SegmentDescriptor.php index 3dc4a973..366eb7ee 100644 --- a/lib/Fhp/Segment/SegmentDescriptor.php +++ b/lib/Fhp/Segment/SegmentDescriptor.php @@ -54,7 +54,7 @@ protected function __construct(string $class) public function validateObject($obj): void // Override { parent::validateObject($obj); - if (!($obj instanceof BaseSegment)) { + if (!$obj instanceof BaseSegment) { throw new \InvalidArgumentException('Expected sub-class of BaseSegment, got ' . gettype($obj)); } if ($obj->getName() !== $this->kennung) { diff --git a/lib/Fhp/Syntax/Parser.php b/lib/Fhp/Syntax/Parser.php index 72620ac0..018d272b 100644 --- a/lib/Fhp/Syntax/Parser.php +++ b/lib/Fhp/Syntax/Parser.php @@ -427,11 +427,13 @@ private static function parseSegmentElement(string $rawElement, ElementDescripto { if (is_string($descriptor->type)) { // Scalar value / DE return static::parseDataElement($rawElement, $descriptor->type); - } elseif ($descriptor->type->getName() === Bin::class) { + } + + if ($descriptor->type->getName() === Bin::class) { return static::parseBinaryBlock($rawElement); - } else { - return static::parseDeg($rawElement, $descriptor->type->name, $descriptor->optional); } + + return static::parseDeg($rawElement, $descriptor->type->name, $descriptor->optional); } /** diff --git a/lib/Fhp/Syntax/Serializer.php b/lib/Fhp/Syntax/Serializer.php index c0d64ff3..c66eb829 100644 --- a/lib/Fhp/Syntax/Serializer.php +++ b/lib/Fhp/Syntax/Serializer.php @@ -32,18 +32,23 @@ public static function serializeDataElement($value, string $type): string if ($value === null) { return ''; } + if ($type === 'int' || $type === 'integer' || $type === 'string') { // Convert UTF-8 (PHP's encoding) to ISO-8859-1 (FinTS wire format encoding) return static::escape(mb_convert_encoding(strval($value), 'ISO-8859-1', 'UTF-8')); - } elseif ($type === 'float') { + } + + if ($type === 'float') { // Format with fixed 2 decimal places (there has to be some limit, and the specification does not specify // one), then trim zeros from the end. return rtrim(number_format($value, 2, ',', ''), '0'); - } elseif ($type === 'bool' || $type === 'boolean') { + } + + if ($type === 'bool' || $type === 'boolean') { return $value ? 'J' : 'N'; - } else { - throw new \RuntimeException("Unsupported type $type"); } + + throw new \RuntimeException("Unsupported type $type"); } /** @@ -147,14 +152,18 @@ private static function serializeElement($value, $type, bool $fullySerialize) { if (is_string($type)) { // Scalar value / DE return static::serializeDataElement($value, $type); - } elseif ($type->getName() === Bin::class) { + } + + if ($type->getName() === Bin::class) { /* @var Bin|null $value */ return $value === null ? '' : $value->toString(); - } elseif ($fullySerialize) { + } + + if ($fullySerialize) { return static::serializeDeg($value, DegDescriptor::get($type->name)); - } else { - return static::serializeElements($value, DegDescriptor::get($type->name)); } + + return static::serializeElements($value, DegDescriptor::get($type->name)); } /**