PHPackages                             icecave/parity - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. icecave/parity

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

icecave/parity
==============

A customizable deep comparison library.

3.0.1(5y ago)516.8M—8.8%4[2 PRs](https://github.com/icecave/parity/pulls)10MITPHPPHP &gt;=7.3

Since May 29Pushed 1y ago2 watchersCompare

[ Source](https://github.com/icecave/parity)[ Packagist](https://packagist.org/packages/icecave/parity)[ Docs](https://github.com/icecave/parity)[ RSS](/packages/icecave-parity/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (6)Versions (10)Used By (10)

Parity
======

[](#parity)

[![Build Status](https://camo.githubusercontent.com/ac791c8ca0ad5bad124e414db756ffd24fc30913f619380059ebfe9fa0c33ccd/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f696365636176652f7061726974792f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/icecave/parity)[![Code Coverage](https://camo.githubusercontent.com/8efe6a70c7f179901f52ca04dda24dfb580efa26230ecad673d5aa2cb023f907/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f696365636176652f7061726974792f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/icecave/parity)[![Latest Version](https://camo.githubusercontent.com/5999ef9b8beb9faf1461b22087d4cb73a1affdb297956b943207b7979e2d9b21/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696365636176652f7061726974792e7376673f7374796c653d666c61742d737175617265266c6162656c3d73656d766572)](https://semver.org)

**Parity** is a deep comparison library for PHP.

```
composer require icecave/parity

```

Rationale
---------

[](#rationale)

PHP does not provide a way to reliably and strictly compare values of heterogeneous types. Most of the built-in comparison operators perform often undesired [type-juggling](http://php.net/manual/en/language.types.type-juggling.php). The one exception is the [strict equality operator](http://php.net/manual/en/language.operators.comparison.php), which has the further caveat that it can only compare objects by their identity. No type-strict mechanism is provided for comparing objects by their properties; nor are there any type-strict versions of the relative comparison operators (less-than, greater-than, etc).

**Parity** aims to fill the void by providing a comparison engine with the following features:

- Type-strict comparison of arrays and objects by their elements
- Recursion-safe comparison of objects
- Natural, type-strict comparison semantics for built-in types
- Powerful mechanisms for classes to customize comparison behavior

Example
-------

[](#example)

The **Parity** comparison engine is accessed via static methods on the `Parity` facade class. These methods accept any types and are guaranteed to produce a deterministic comparison result[1](#caveat1). Some basic examples are shown below using integers.

```
use Icecave\Parity\Parity;

// The compare() method provides a strcmp-style comparison, and hence can be
// used as a sorting function for operations such as usort()
assert(Parity::compare(1, 2) < 0);

// The following methods are convenience methods, implemented on top of compare().
assert(Parity::isEqualTo(1, 2) === false);
assert(Parity::isNotEqualTo(1, 2) === true);
assert(Parity::isNotEqualTo(1, 2) === true);
assert(Parity::isLessThan(1, 2) === true);
assert(Parity::isLessThanOrEqualTo(1, 2) === true);
assert(Parity::isGreaterThan(1, 2) === false);
assert(Parity::isGreaterThanOrEqualTo(1, 2) === false);
```

Concepts
--------

[](#concepts)

### Comparable

[](#comparable)

The core concept of **Parity** is the *comparable*. A comparable is any object that provides its own behavior for comparison with other values. The following refinements of the comparable concept are supported by the comparison engine:

- [Restricted Comparable](src/RestrictedComparable.php): A comparable that can be queried as to which values it may be compared to.
- [Self Comparable](src/SelfComparable.php): A comparable that may only be compared to other objects of exactly the same type.
- [Sub Class Comparable](src/SubClassComparable.php): A comparable that may only be compared to other objects of the same or a derived type.
- [Any Comparable](src/AnyComparable.php): A comparable that may be freely compared to values of any other type.

### Comparator

[](#comparator)

A *[Comparator](src/Comparator/Comparator.php)* defines comparison behavior for values other than itself. **Parity** provides the following comparator implementations:

- [Parity Comparator](src/Comparator/ParityComparator.php): Implements the logic surrounding the comparable concepts described in the section above.
- [Deep Comparator](src/Comparator/DeepComparator.php): Performs deep comparison of arrays and objects. Object comparison is recursion-safe.
- [Object Identity Comparator](src/Comparator/ObjectIdentityComparator.php): Compares objects by identity.
- [Strict PHP Comparator](src/Comparator/StrictPhpComparator.php): Approximates PHP's strict comparison for the full suite of comparison operations.
- [PHP Comparator](src/Comparator/PhpComparator.php): Exposes the standard PHP comparison behavior as a Parity comparator.

Algorithm Resolution
--------------------

[](#algorithm-resolution)

The following process is used by `Parity::compare($A, $B)` to determine which comparison algorithm to use:

Use `$A->compare($B)` if:

1. `$A` is [Any Comparable](src/AnyComparable.php); or
2. `$A` is [Restricted Comparable](src/RestrictedComparable.php) and `$A->canCompare($B)` returns `true`; or
3. `$A` is [Self Comparable](src/SelfComparable.php) and `$A` is exactly the same type as `$B`; or
4. `$A` is [Sub Class Comparable](src/SubClassComparable.php) and `$B` is an instance of the class where `$A->compare()` is implemented

If none of the conditions above are true, the comparison is tried in reverse with `$A` on the right hand side and `$B`on the left; the result is also inverted. If still no comparison is possible, **Parity** falls back to a strictly-typed deep comparison.

When comparing scalar types, integers and doubles (PHP's only true numeric types) are treated as though they were the same type, such that the expression `3 < 3.5 < 4` holds true. Numeric strings are **not** compared in this way.

Caveats
-------

[](#caveats)

1. Comparison of recursive objects is not a truly deterministic operation as objects are compared by their [object hash](http://php.net/manual/en/function.spl-object-hash.php) where deeper comparison would otherwise result in infinite recursion.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity57

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 93.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~468 days

Recently: every ~665 days

Total

7

Last Release

1930d ago

Major Versions

0.1.0 → 1.0.0-alpha.12013-10-20

1.0.0 → 2.0.02018-11-06

2.0.0 → 3.0.02020-08-25

PHP version history (4 changes)0.1.0PHP &gt;=5.3

2.0.0PHP ^7

3.0.0PHP ^7.2

3.0.1PHP &gt;=7.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/93a71bd75fcd51efee464532dbdd54927cd00e938805998c76e0a804d38fa3fb?d=identicon)[jmalloc](/maintainers/jmalloc)

---

Top Contributors

[![jmalloc](https://avatars.githubusercontent.com/u/761536?v=4)](https://github.com/jmalloc "jmalloc (101 commits)")[![koden-km](https://avatars.githubusercontent.com/u/1037307?v=4)](https://github.com/koden-km "koden-km (7 commits)")

---

Tags

equalitycomparelesssortcomparisonsortingequalgreater

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/icecave-parity/health.svg)

```
[![Health](https://phpackages.com/badges/icecave-parity/health.svg)](https://phpackages.com/packages/icecave-parity)
```

###  Alternatives

[kyslik/column-sortable

Package for handling column sorting in Laravel 6.x

6485.6M21](/packages/kyslik-column-sortable)[mottie/tablesorter

tablesorter (FORK) is a jQuery plugin for turning a standard HTML table with THEAD and TBODY tags into a sortable table without page refreshes. tablesorter can successfully parse and sort many types of data including linked data in a cell.

2.6k223.5k](/packages/mottie-tablesorter)[scssphp/scssphp

scssphp is a compiler for SCSS written in PHP.

62827.7M220](/packages/scssphp-scssphp)[wikimedia/less.php

PHP port of the LESS processor

12327.4M77](/packages/wikimedia-lessphp)[tuck/sort

Syntactic sugar for PHP's sorting

11547.3k](/packages/tuck-sort)[topshelfcraft/supersort

...a super-duper sorting function for your Craft templates.

4287.1k1](/packages/topshelfcraft-supersort)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
