From c1737af56bdb81fe00d08e5c08c77e123ee88319 Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Tue, 7 Mar 2017 07:59:51 +0100 Subject: [PATCH] Added scalar and return type hints --- src/ArrayComparer/OrderedArrayComparer.php | 2 +- src/DiffOp/Diff/Diff.php | 4 ++-- src/DiffOp/DiffOp.php | 2 +- src/DiffOp/DiffOpAdd.php | 2 +- src/DiffOp/DiffOpChange.php | 2 +- src/DiffOp/DiffOpRemove.php | 2 +- src/Differ/ListDiffer.php | 4 ++-- src/Differ/MapDiffer.php | 19 +++++++++--------- src/Patcher/MapPatcher.php | 23 ++++++++++------------ src/Patcher/ThrowingPatcher.php | 6 +++--- 10 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/ArrayComparer/OrderedArrayComparer.php b/src/ArrayComparer/OrderedArrayComparer.php index b1e7443..466a12b 100644 --- a/src/ArrayComparer/OrderedArrayComparer.php +++ b/src/ArrayComparer/OrderedArrayComparer.php @@ -58,7 +58,7 @@ public function diffArrays( array $arrayOne, array $arrayTwo ): array { * * @return bool */ - private function arraySearch( $needle, array $haystack, $valueOffset ) { + private function arraySearch( $needle, array $haystack, $valueOffset ): bool { if ( array_key_exists( $valueOffset, $haystack ) ) { return $this->valueComparer->valuesAreEqual( $needle, $haystack[$valueOffset] ); } diff --git a/src/DiffOp/Diff/Diff.php b/src/DiffOp/Diff/Diff.php index 276f303..8fceb68 100644 --- a/src/DiffOp/Diff/Diff.php +++ b/src/DiffOp/Diff/Diff.php @@ -88,7 +88,7 @@ public function getOperations(): array { * * @return DiffOp[] */ - public function getTypeOperations( $type ): array { + public function getTypeOperations( string $type ): array { return array_intersect_key( $this->getArrayCopy(), array_flip( $this->typePointers[$type] ) @@ -335,7 +335,7 @@ public function hasAssociativeOperations(): bool { * * @return array */ - public function toArray( $valueConverter = null ): array { + public function toArray( callable $valueConverter = null ): array { $operations = array(); foreach ( $this->getOperations() as $key => $diffOp ) { diff --git a/src/DiffOp/DiffOp.php b/src/DiffOp/DiffOp.php index 011aa13..bbb9c89 100644 --- a/src/DiffOp/DiffOp.php +++ b/src/DiffOp/DiffOp.php @@ -57,6 +57,6 @@ public function isAtomic(): bool; * * @return array */ - public function toArray( $valueConverter = null ): array; + public function toArray( callable $valueConverter = null ): array; } diff --git a/src/DiffOp/DiffOpAdd.php b/src/DiffOp/DiffOpAdd.php index e124361..ee8efef 100644 --- a/src/DiffOp/DiffOpAdd.php +++ b/src/DiffOp/DiffOpAdd.php @@ -76,7 +76,7 @@ public function unserialize( $serialization ) { * * @return array */ - public function toArray( $valueConverter = null ): array { + public function toArray( callable $valueConverter = null ): array { return array( 'type' => $this->getType(), 'newvalue' => $this->objectToArray( $this->newValue, $valueConverter ), diff --git a/src/DiffOp/DiffOpChange.php b/src/DiffOp/DiffOpChange.php index ffc0d8a..e14b980 100644 --- a/src/DiffOp/DiffOpChange.php +++ b/src/DiffOp/DiffOpChange.php @@ -88,7 +88,7 @@ public function unserialize( $serialization ) { * * @return array */ - public function toArray( $valueConverter = null ): array { + public function toArray( callable $valueConverter = null ): array { return array( 'type' => $this->getType(), 'newvalue' => $this->objectToArray( $this->newValue, $valueConverter ), diff --git a/src/DiffOp/DiffOpRemove.php b/src/DiffOp/DiffOpRemove.php index 8beca19..4f23929 100644 --- a/src/DiffOp/DiffOpRemove.php +++ b/src/DiffOp/DiffOpRemove.php @@ -76,7 +76,7 @@ public function unserialize( $serialization ) { * * @return array */ - public function toArray( $valueConverter = null ): array { + public function toArray( callable $valueConverter = null ): array { return array( 'type' => $this->getType(), 'oldvalue' => $this->objectToArray( $this->oldValue, $valueConverter ), diff --git a/src/Differ/ListDiffer.php b/src/Differ/ListDiffer.php index e74c385..9f159d7 100644 --- a/src/Differ/ListDiffer.php +++ b/src/Differ/ListDiffer.php @@ -65,7 +65,7 @@ public function __construct( $arrayComparer = null ) { * @return ArrayComparer * @throws InvalidArgumentException */ - private function getRealArrayComparer( $arrayComparer ) { + private function getRealArrayComparer( $arrayComparer ): ArrayComparer { if ( $arrayComparer === null || $arrayComparer === self::MODE_STRICT ) { return new StrictArrayComparer(); } @@ -111,7 +111,7 @@ public function doDiff( array $oldValues, array $newValues ): array { * * @return array */ - private function diffArrays( array $arrayOne, array $arrayTwo ) { + private function diffArrays( array $arrayOne, array $arrayTwo ): array { return $this->arrayComparer->diffArrays( $arrayOne, $arrayTwo ); } diff --git a/src/Differ/MapDiffer.php b/src/Differ/MapDiffer.php index 285f8aa..3061eba 100644 --- a/src/Differ/MapDiffer.php +++ b/src/Differ/MapDiffer.php @@ -94,7 +94,7 @@ public function doDiff( array $oldValues, array $newValues ): array { return $diffSet; } - private function getAllKeys( $oldSet, $newSet ) { + private function getAllKeys( $oldSet, $newSet ): array { return array_unique( array_merge( array_keys( $oldSet ), array_keys( $newSet ) @@ -138,20 +138,19 @@ private function getDiffOpForElementRecursively( $key, array $oldSet, array $new $new = array_key_exists( $key, $newSet ) ? $newSet[$key] : array(); if ( is_array( $old ) && is_array( $new ) ) { - $diff = $this->getDiffForArrays( $old, $new ); - return $diff; + return $this->getDiffForArrays( $old, $new ); } return null; } - private function getDiffForArrays( array $old, array $new ) { + private function getDiffForArrays( array $old, array $new ): Diff { if ( $this->isAssociative( $old ) || $this->isAssociative( $new ) ) { return new Diff( $this->doDiff( $old, $new ), true ); } - else { - return new Diff( $this->listDiffer->doDiff( $old, $new ), false ); - } + + return new Diff( $this->listDiffer->doDiff( $old, $new ), false ); + } /** @@ -161,7 +160,7 @@ private function getDiffForArrays( array $old, array $new ) { * * @return bool */ - private function isAssociative( array $array ) { + private function isAssociative( array $array ): bool { foreach ( $array as $key => $value ) { if ( is_string( $key ) ) { return true; @@ -183,7 +182,7 @@ private function isAssociative( array $array ) { * * @return array */ - private function arrayDiffAssoc( array $from, array $to ) { + private function arrayDiffAssoc( array $from, array $to ): array { $diff = array(); foreach ( $from as $key => $value ) { @@ -202,7 +201,7 @@ private function arrayDiffAssoc( array $from, array $to ) { * @return bool * @throws Exception */ - private function valuesAreEqual( $value0, $value1 ) { + private function valuesAreEqual( $value0, $value1 ): bool { if ( $this->comparisonCallback === null ) { return $value0 === $value1; } diff --git a/src/Patcher/MapPatcher.php b/src/Patcher/MapPatcher.php index 56d9d3a..38bad71 100644 --- a/src/Patcher/MapPatcher.php +++ b/src/Patcher/MapPatcher.php @@ -36,7 +36,7 @@ class MapPatcher extends ThrowingPatcher { * @param bool $throwErrors * @param Patcher|null $listPatcher The patcher that will be used for lists in the value */ - public function __construct( $throwErrors = false, Patcher $listPatcher = null ) { + public function __construct( bool $throwErrors = false, Patcher $listPatcher = null ) { parent::__construct( $throwErrors ); $this->listPatcher = $listPatcher ?: new ListPatcher( $throwErrors ); @@ -75,7 +75,7 @@ public function patch( array $base, Diff $diff ): array { * * @throws PatcherException */ - private function applyOperation( &$base, $key, DiffOp $diffOp ) { + private function applyOperation( array &$base, $key, DiffOp $diffOp ) { if ( $diffOp instanceof DiffOpAdd ) { $this->applyDiffOpAdd( $base, $key, $diffOp ); } @@ -100,7 +100,7 @@ private function applyOperation( &$base, $key, DiffOp $diffOp ) { * * @throws PatcherException */ - private function applyDiffOpAdd( &$base, $key, DiffOpAdd $diffOp ) { + private function applyDiffOpAdd( array &$base, $key, DiffOpAdd $diffOp ) { if ( array_key_exists( $key, $base ) ) { $this->handleError( 'Cannot add an element already present in a map' ); return; @@ -116,7 +116,7 @@ private function applyDiffOpAdd( &$base, $key, DiffOpAdd $diffOp ) { * * @throws PatcherException */ - private function applyDiffOpRemove( &$base, $key, DiffOpRemove $diffOp ) { + private function applyDiffOpRemove( array &$base, $key, DiffOpRemove $diffOp ) { if ( !array_key_exists( $key, $base ) ) { $this->handleError( 'Cannot do a non-add operation with an element not present in a map' ); return; @@ -137,7 +137,7 @@ private function applyDiffOpRemove( &$base, $key, DiffOpRemove $diffOp ) { * * @throws PatcherException */ - private function applyDiffOpChange( &$base, $key, DiffOpChange $diffOp ) { + private function applyDiffOpChange( array &$base, $key, DiffOpChange $diffOp ) { if ( !array_key_exists( $key, $base ) ) { $this->handleError( 'Cannot do a non-add operation with an element not present in a map' ); return; @@ -178,7 +178,7 @@ private function applyDiff( &$base, $key, Diff $diffOp ) { * * @return bool */ - private function isAttemptToModifyNotExistingElement( $base, $key, Diff $diffOp ) { + private function isAttemptToModifyNotExistingElement( $base, $key, Diff $diffOp ): bool { return !array_key_exists( $key, $base ) && ( $diffOp->getChanges() !== array() || $diffOp->getRemovals() !== array() ); } @@ -189,15 +189,12 @@ private function isAttemptToModifyNotExistingElement( $base, $key, Diff $diffOp * * @return array */ - private function patchMapOrList( array $base, Diff $diff ) { + private function patchMapOrList( array $base, Diff $diff ): array { if ( $diff->looksAssociative() ) { - $base = $this->patch( $base, $diff ); - } - else { - $base = $this->listPatcher->patch( $base, $diff ); + return $this->patch( $base, $diff ); } - return $base; + return $this->listPatcher->patch( $base, $diff ); } /** @@ -206,7 +203,7 @@ private function patchMapOrList( array $base, Diff $diff ) { * * @return bool */ - private function valuesAreEqual( $firstValue, $secondValue ) { + private function valuesAreEqual( $firstValue, $secondValue ): bool { if ( $this->comparer === null ) { $this->comparer = new StrictComparer(); } diff --git a/src/Patcher/ThrowingPatcher.php b/src/Patcher/ThrowingPatcher.php index e45967f..887bbc5 100644 --- a/src/Patcher/ThrowingPatcher.php +++ b/src/Patcher/ThrowingPatcher.php @@ -28,7 +28,7 @@ abstract class ThrowingPatcher implements PreviewablePatcher { * * @param bool $throwErrors */ - public function __construct( $throwErrors = false ) { + public function __construct( bool $throwErrors = false ) { $this->throwErrors = $throwErrors; } @@ -39,7 +39,7 @@ public function __construct( $throwErrors = false ) { * * @throws PatcherException */ - protected function handleError( $message ) { + protected function handleError( string $message ) { if ( $this->throwErrors ) { throw new PatcherException( $message ); } @@ -74,7 +74,7 @@ public function throwErrors() { * @return Diff * @throws PatcherException */ - public function getApplicableDiff( array $base, Diff $diff ) { + public function getApplicableDiff( array $base, Diff $diff ): Diff { $throwErrors = $this->throwErrors; $this->throwErrors = false;