Skip to content

Commit

Permalink
Added scalar and return type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Mar 7, 2017
1 parent bd9b4c8 commit c1737af
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/ArrayComparer/OrderedArrayComparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] );
}
Expand Down
4 changes: 2 additions & 2 deletions src/DiffOp/Diff/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] )
Expand Down Expand Up @@ -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 ) {
Expand Down
2 changes: 1 addition & 1 deletion src/DiffOp/DiffOp.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public function isAtomic(): bool;
*
* @return array
*/
public function toArray( $valueConverter = null ): array;
public function toArray( callable $valueConverter = null ): array;

}
2 changes: 1 addition & 1 deletion src/DiffOp/DiffOpAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down
2 changes: 1 addition & 1 deletion src/DiffOp/DiffOpChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down
2 changes: 1 addition & 1 deletion src/DiffOp/DiffOpRemove.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down
4 changes: 2 additions & 2 deletions src/Differ/ListDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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 );
}

Expand Down
19 changes: 9 additions & 10 deletions src/Differ/MapDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );

}

/**
Expand All @@ -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;
Expand All @@ -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 ) {
Expand All @@ -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;
}
Expand Down
23 changes: 10 additions & 13 deletions src/Patcher/MapPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() );
}
Expand All @@ -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 );
}

/**
Expand All @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Patcher/ThrowingPatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 );
}
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit c1737af

Please sign in to comment.