Skip to content

Commit

Permalink
Support [email protected], PHP 8.4 readiness
Browse files Browse the repository at this point in the history
This repository is currently using version 34 of the Mediawiki
coding style phpcs rules. It should be upgraded to the latest version.

Besides being simply different, the newer versions of the rules
introduce, for example,
MediaWiki.Usage.NullableType.ExplicitNullableTypes, which enforces
compliance with coming deprecation rules in PHP 8.4 concerning nullable
types. If the code does not remove the soon-to-be-deprecated form of
nullable type declarations, the library will no longer be usable in
Mediawiki for PHP 8.4 deployments.

Replace existing phpcs rules with the standard mediawiki-codesniffer
ruleset.

Bug: T379481
  • Loading branch information
codders committed Dec 6, 2024
1 parent 4bb3d80 commit 935f38f
Show file tree
Hide file tree
Showing 38 changed files with 1,248 additions and 1,298 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
},
"require-dev": {
"phpunit/phpunit": "~8.5.0",
"squizlabs/php_codesniffer": "~3.6.0",
"ockcyp/covers-validator": "~1.0"
"ockcyp/covers-validator": "~1.0",
"mediawiki/mediawiki-codesniffer": "^45"
},
"autoload": {
"files" : [
Expand Down Expand Up @@ -65,5 +65,10 @@
"phpcs": [
"vendor/bin/phpcs src/* tests/* --standard=phpcs.xml --extensions=php -sp"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
102 changes: 1 addition & 101 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,5 @@
- https://github.com/squizlabs/PHP_CodeSniffer/tree/master/CodeSniffer/Standards
-->
<ruleset name="Diff">
<rule ref="Generic.Classes" />
<rule ref="Generic.CodeAnalysis" />
<rule ref="Generic.ControlStructures" />

<rule ref="Generic.Files.ByteOrderMark" />
<rule ref="Generic.Files.EndFileNewline" />
<rule ref="Generic.Files.InlineHTML" />
<rule ref="Generic.Files.LineEndings" />
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120" />
<property name="absoluteLineLimit" value="120" />
</properties>
</rule>
<rule ref="Generic.Files.OneClassPerFile" />
<rule ref="Generic.Files.OneInterfacePerFile" />
<rule ref="Generic.Files.OneTraitPerFile" />

<rule ref="Generic.Formatting.DisallowMultipleStatements" />

<rule ref="Generic.Functions.CallTimePassByReference" />
<rule ref="Generic.Functions.FunctionCallArgumentSpacing" />
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />

<rule ref="Generic.Metrics.NestingLevel">
<properties>
<property name="nestingLevel" value="3" />
<property name="absoluteNestingLevel" value="3" />
</properties>
</rule>

<rule ref="Generic.Metrics.CyclomaticComplexity">
<properties>
<property name="complexity" value="10" />
<property name="absoluteComplexity" value="10" />
</properties>
</rule>

<rule ref="Generic.NamingConventions">
<exclude name="Generic.NamingConventions.AbstractClassNamePrefix"/>
<exclude name="Generic.NamingConventions.InterfaceNameSuffix"/>
</rule>
<rule ref="Generic.NamingConventions.CamelCapsFunctionName.ScopeNotCamelCaps">
<!-- Exclude test methods like "testGivenInvalidInput_methodThrowsException". -->
<exclude-pattern>tests.unit*Test\.php</exclude-pattern>
</rule>

<rule ref="Generic.PHP.CharacterBeforePHPOpeningTag" />
<rule ref="Generic.PHP.DeprecatedFunctions" />
<rule ref="Generic.PHP.DisallowShortOpenTag" />
<rule ref="Generic.PHP.ForbiddenFunctions" />
<rule ref="Generic.PHP.LowerCaseConstant" />
<rule ref="Generic.PHP.LowerCaseKeyword" />
<rule ref="Generic.PHP.NoSilencedErrors" />
<rule ref="Generic.PHP.SAPIUsage" />

<rule ref="Generic.WhiteSpace.DisallowSpaceIndent" />

<rule ref="PSR1" />
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
<!-- Exclude test methods like "testGivenInvalidInput_methodThrowsException". -->
<exclude-pattern>tests.unit*Test\.php</exclude-pattern>
</rule>

<rule ref="PSR2.Classes.PropertyDeclaration" />
<rule ref="PSR2.ControlStructures.ElseIfDeclaration" />
<rule ref="PSR2.Files" />
<rule ref="PSR2.Namespaces" />

<rule ref="Squiz.Arrays.ArrayBracketSpacing" />
<rule ref="Squiz.CSS.SemicolonSpacing" />
<rule ref="Squiz.Classes.DuplicateProperty" />
<rule ref="Squiz.Classes.SelfMemberReference" />
<rule ref="Squiz.Classes.ValidClassName" />
<rule ref="Squiz.Functions.FunctionDuplicateArgument" />
<rule ref="Squiz.Functions.GlobalFunction" />
<rule ref="Squiz.Scope" />

<rule ref="Squiz.Strings.DoubleQuoteUsage">
<exclude name="Squiz.Strings.DoubleQuoteUsage.ContainsVar" />
</rule>

<rule ref="Squiz.WhiteSpace.CastSpacing" />
<rule ref="Squiz.WhiteSpace.LanguageConstructSpacing" />
<rule ref="Squiz.WhiteSpace.LogicalOperatorSpacing" />
<rule ref="Squiz.WhiteSpace.FunctionSpacing">
<properties>
<property name="spacing" value="1" />
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.OperatorSpacing">
<properties>
<property name="ignoreNewlines" value="true" />
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace" />
<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing" />
<rule ref="Squiz.WhiteSpace.SemicolonSpacing" />
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace" />

<rule ref="Zend.Files.ClosingTag" />
<rule ref="./vendor/mediawiki/mediawiki-codesniffer/MediaWiki" />
</ruleset>
2 changes: 1 addition & 1 deletion src/ArrayComparer/OrderedArrayComparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class OrderedArrayComparer implements ArrayComparer {

private $valueComparer;
private ValueComparer $valueComparer;

public function __construct( ValueComparer $valueComparer ) {
$this->valueComparer = $valueComparer;
Expand Down
2 changes: 1 addition & 1 deletion src/ArrayComparer/StrategicArrayComparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class StrategicArrayComparer implements ArrayComparer {

private $valueComparer;
private ValueComparer $valueComparer;

public function __construct( ValueComparer $valueComparer ) {
$this->valueComparer = $valueComparer;
Expand Down
3 changes: 2 additions & 1 deletion src/Comparer/CallbackComparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class CallbackComparer implements ValueComparer {

/** @var callable */
private $callback;

/**
Expand All @@ -26,7 +27,7 @@ public function __construct( $callback ) {
$this->callback = $callback;
}

public function valuesAreEqual( $firstValue, $secondValue ): bool {
public function valuesAreEqual( mixed $firstValue, mixed $secondValue ): bool {
$valuesAreEqual = call_user_func_array( $this->callback, [ $firstValue, $secondValue ] );

if ( !is_bool( $valuesAreEqual ) ) {
Expand Down
5 changes: 5 additions & 0 deletions src/Comparer/ComparableComparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
class ComparableComparer implements ValueComparer {

/**
* @param mixed $firstValue
* @param mixed $secondValue
* @return bool
*/
public function valuesAreEqual( $firstValue, $secondValue ): bool {
if ( $firstValue && method_exists( $firstValue, 'equals' ) ) {
return $firstValue->equals( $secondValue );
Expand Down
23 changes: 14 additions & 9 deletions src/DiffOp/Diff/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public function __construct( array $operations = [], $isAssociative = null ) {

foreach ( $operations as $offset => $operation ) {
if ( !( $operation instanceof DiffOp ) ) {
throw new InvalidArgumentException( 'All elements fed to the Diff constructor should be of type DiffOp' );
throw new InvalidArgumentException(
'All elements fed to the Diff constructor should be of type DiffOp'
);
}

$this->offsetSet( $offset, $operation );
Expand Down Expand Up @@ -125,14 +127,17 @@ public function addOperations( array $operations ) {
*/
private function preSetElement( $index, DiffOp $value ): bool {
if ( $this->isAssociative === false && ( $value->getType() !== 'add' && $value->getType() !== 'remove' ) ) {
throw new InvalidArgumentException( 'Diff operation with invalid type "' . $value->getType() . '" provided.' );
throw new InvalidArgumentException(
'Diff operation with invalid type "' . $value->getType() . '" provided.'
);
}

if ( array_key_exists( $value->getType(), $this->typePointers ) ) {
$this->typePointers[$value->getType()][] = $index;
}
else {
throw new InvalidArgumentException( 'Diff operation with invalid type "' . $value->getType() . '" provided.' );
} else {
throw new InvalidArgumentException(
'Diff operation with invalid type "' . $value->getType() . '" provided.'
);
}

return true;
Expand All @@ -147,7 +152,7 @@ private function preSetElement( $index, DiffOp $value ): bool {
*/
#[\ReturnTypeWillChange]
public function unserialize( $serialization ) {
$this->__unserialize( unserialize( $serialization) );
$this->__unserialize( unserialize( $serialization ) );
}

/**
Expand Down Expand Up @@ -205,7 +210,7 @@ public function getChanges(): array {
*/
public function getAddedValues(): array {
return array_map(
function( DiffOpAdd $addition ) {
static function ( DiffOpAdd $addition ) {
return $addition->getNewValue();
},
$this->getTypeOperations( 'add' )
Expand All @@ -219,7 +224,7 @@ function( DiffOpAdd $addition ) {
*/
public function getRemovedValues(): array {
return array_map(
function( DiffOpRemove $addition ) {
static function ( DiffOpRemove $addition ) {
return $addition->getOldValue();
},
$this->getTypeOperations( 'remove' )
Expand Down Expand Up @@ -335,7 +340,7 @@ public function hasAssociativeOperations(): bool {
*
* @return array
*/
public function toArray( callable $valueConverter = null ): array {
public function toArray( ?callable $valueConverter = null ): array {
$operations = [];

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 @@ -59,6 +59,6 @@ public function isAtomic(): bool;
*
* @return array
*/
public function toArray( callable $valueConverter = null ): array;
public function toArray( ?callable $valueConverter = null ): array;

}
3 changes: 2 additions & 1 deletion src/DiffOp/DiffOpAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class DiffOpAdd extends AtomicDiffOp {

/** @var mixed */
private $newValue;

/**
Expand Down Expand Up @@ -98,7 +99,7 @@ public function __unserialize( $data ): void {
*
* @return array
*/
public function toArray( callable $valueConverter = null ): array {
public function toArray( ?callable $valueConverter = null ): array {
return [
'type' => $this->getType(),
'newvalue' => $this->objectToArray( $this->newValue, $valueConverter ),
Expand Down
6 changes: 4 additions & 2 deletions src/DiffOp/DiffOpChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
class DiffOpChange extends AtomicDiffOp {

/** @var mixed */
private $newValue;
/** @var mixed */
private $oldValue;

/**
Expand Down Expand Up @@ -88,7 +90,7 @@ public function __serialize(): array {
*/
#[\ReturnTypeWillChange]
public function unserialize( $serialization ) {
$this->__unserialize( unserialize ($serialization) );
$this->__unserialize( unserialize( $serialization ) );
}

/**
Expand All @@ -110,7 +112,7 @@ public function __unserialize( $data ): void {
*
* @return array
*/
public function toArray( callable $valueConverter = null ): array {
public function toArray( ?callable $valueConverter = null ): array {
return [
'type' => $this->getType(),
'newvalue' => $this->objectToArray( $this->newValue, $valueConverter ),
Expand Down
3 changes: 2 additions & 1 deletion src/DiffOp/DiffOpRemove.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class DiffOpRemove extends AtomicDiffOp {

/** @var mixed */
private $oldValue;

/**
Expand Down Expand Up @@ -98,7 +99,7 @@ public function __unserialize( $data ): void {
*
* @return array
*/
public function toArray( callable $valueConverter = null ): array {
public function toArray( ?callable $valueConverter = null ): array {
return [
'type' => $this->getType(),
'oldvalue' => $this->objectToArray( $this->oldValue, $valueConverter ),
Expand Down
2 changes: 1 addition & 1 deletion src/Differ/ListDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ListDiffer implements Differ {
*/
private $arrayComparer;

public function __construct( ArrayComparer $arrayComparer = null ) {
public function __construct( ?ArrayComparer $arrayComparer = null ) {
$this->arrayComparer = $arrayComparer ?? new StrictArrayComparer();
}

Expand Down
24 changes: 19 additions & 5 deletions src/Differ/MapDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class MapDiffer implements Differ {
/**
* The third argument ($comparer) was added in 3.0
*/
public function __construct( bool $recursively = false, Differ $listDiffer = null, ValueComparer $comparer = null ) {
public function __construct(
bool $recursively = false,
?Differ $listDiffer = null,
?ValueComparer $comparer = null
) {
$this->recursively = $recursively;
$this->listDiffer = $listDiffer ?? new ListDiffer();
$this->valueComparer = $comparer ?? new StrictComparer();
Expand Down Expand Up @@ -86,6 +90,12 @@ private function getAllKeys( array $oldSet, array $newSet ): array {
) );
}

/**
* @param mixed $key
* @param array $oldSet
* @param mixed $newSet
* @return DiffOp
*/
private function getDiffOpForElement( $key, array $oldSet, array $newSet ) {
if ( $this->recursively ) {
$diffOp = $this->getDiffOpForElementRecursively( $key, $oldSet, $newSet );
Expand All @@ -105,11 +115,9 @@ private function getDiffOpForElement( $key, array $oldSet, array $newSet ) {

if ( $hasOld && $hasNew ) {
return new DiffOpChange( $oldSet[$key], $newSet[$key] );
}
elseif ( $hasOld ) {
} elseif ( $hasOld ) {
return new DiffOpRemove( $oldSet[$key] );
}
elseif ( $hasNew ) {
} elseif ( $hasNew ) {
return new DiffOpAdd( $newSet[$key] );
}

Expand All @@ -118,6 +126,12 @@ private function getDiffOpForElement( $key, array $oldSet, array $newSet ) {
// @codeCoverageIgnoreEnd
}

/**
* @param mixed $key
* @param array $oldSet
* @param mixed $newSet
* @return ?Diff
*/
private function getDiffOpForElementRecursively( $key, array $oldSet, array $newSet ) {
$old = array_key_exists( $key, $oldSet ) ? $oldSet[$key] : [];
$new = array_key_exists( $key, $newSet ) ? $newSet[$key] : [];
Expand Down
Loading

0 comments on commit 935f38f

Please sign in to comment.