Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
More linters, and strict everything
Browse files Browse the repository at this point in the history
  • Loading branch information
fredemmott committed Feb 14, 2019
1 parent a775091 commit 2287da6
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 201 deletions.
1 change: 1 addition & 0 deletions hhast-lint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"builtinLinters": "all",
"roots": [ "src/", "tests/" ]
}
126 changes: 67 additions & 59 deletions src/Assert.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?hh
<?hh // strict
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
Expand All @@ -16,7 +16,11 @@

abstract class Assert {

public function assertSame($expected, $actual, string $message = ''): void {
public function assertSame(
mixed $expected,
mixed $actual,
string $message = '',
): void {
if ($expected === $actual) {
return;
}
Expand All @@ -42,8 +46,8 @@ public function assertSame($expected, $actual, string $message = ''): void {
}

public function assertNotSame(
$expected,
$actual,
mixed $expected,
mixed $actual,
string $message = '',
): void {
if ($expected !== $actual) {
Expand All @@ -59,7 +63,11 @@ public function assertNotSame(
);
}

public function assertEquals($expected, $actual, string $message = ''): void {
public function assertEquals(
mixed $expected,
mixed $actual,
string $message = '',
): void {
/* HHAST_IGNORE_ERROR[NoPHPEquality] */
if ($actual == $expected) {
return;
Expand All @@ -76,7 +84,7 @@ public function assertEquals($expected, $actual, string $message = ''): void {

public function assertEqualsWithDelta(
num $expected,
$actual,
num $actual,
float $delta,
string $message = '',
): void {
Expand All @@ -87,16 +95,16 @@ public function assertEqualsWithDelta(
Str\format(
"%s\n%s does not equal %f with delta %f",
$message,
$actual,
(string)$actual,
(float)$expected,
$delta,
),
);
}

public function assertNotEquals(
$expected,
$actual,
mixed $expected,
mixed $actual,
string $message = '',
): void {
/* HHAST_IGNORE_ERROR[NoPHPEquality] */
Expand All @@ -113,7 +121,7 @@ public function assertNotEquals(
);
}

public function assertTrue($condition, string $message = '') {
public function assertTrue(mixed $condition, string $message = ''): void {
if ($condition === true) {
return;
}
Expand All @@ -126,7 +134,7 @@ public function assertTrue($condition, string $message = '') {
);
}

public function assertFalse($condition, string $message = '') {
public function assertFalse(mixed $condition, string $message = ''): void {
if ($condition === false) {
return;
}
Expand All @@ -139,7 +147,7 @@ public function assertFalse($condition, string $message = '') {
);
}

public function assertNull($actual, string $message = '') {
public function assertNull(mixed $actual, string $message = ''): void {
if ($actual === null) {
return;
}
Expand All @@ -152,7 +160,7 @@ public function assertNull($actual, string $message = '') {
);
}

public function assertNotNull($actual, string $message = '') {
public function assertNotNull(mixed $actual, string $message = ''): void {
if ($actual !== null) {
return;
}
Expand All @@ -165,7 +173,8 @@ public function assertNotNull($actual, string $message = '') {
);
}

public function assertEmpty($actual, string $message = '') {
public function assertEmpty(mixed $actual, string $message = ''): void {
/* HH_FIXME[4016] PHPism */
if (empty($actual)) {
return;
}
Expand All @@ -178,8 +187,9 @@ public function assertEmpty($actual, string $message = '') {
);
}

public function assertNotEmpty($actual, string $message = '') {
if (empty($actual)) {
public function assertNotEmpty(mixed $actual, string $message = ''): void {
/* HH_FIXME[4016] PHPism */
if (!empty($actual)) {
return;
}
throw new ExpectationFailedException(
Expand All @@ -192,8 +202,8 @@ public function assertNotEmpty($actual, string $message = '') {
}

public function assertGreaterThan(
$expected,
$actual,
num $expected,
num $actual,
string $message = '',
): void {
if ($actual > $expected) {
Expand All @@ -210,8 +220,8 @@ public function assertGreaterThan(
}

public function assertLessThan(
$expected,
$actual,
num $expected,
num $actual,
string $message = '',
): void {
if ($actual < $expected) {
Expand All @@ -228,8 +238,8 @@ public function assertLessThan(
}

public function assertGreaterThanOrEqual(
$expected,
$actual,
num $expected,
num $actual,
string $message = '',
): void {
if ($actual >= $expected) {
Expand All @@ -246,8 +256,8 @@ public function assertGreaterThanOrEqual(
}

public function assertLessThanOrEqual(
$expected,
$actual,
num $expected,
num $actual,
string $message = '',
): void {
if ($actual <= $expected) {
Expand All @@ -265,13 +275,13 @@ public function assertLessThanOrEqual(

public function assertInstanceOf(
string $expected,
$actual,
mixed $actual,
string $message = '',
): void {
if (!\class_exists($expected) && !\interface_exists($expected)) {
throw new InvalidArgumentException('Invalid class or interface name');
}
if ($actual instanceof $expected) {
if (\is_a($actual, $expected)) {
return;
}
throw new ExpectationFailedException(
Expand All @@ -286,13 +296,13 @@ public function assertInstanceOf(

public function assertNotInstanceOf(
string $expected,
$actual,
mixed $actual,
string $message = '',
): void {
if (!\class_exists($expected) && !\interface_exists($expected)) {
throw new InvalidArgumentException('Invalid class or interface name');
}
if (!($actual instanceof $expected)) {
if (!\is_a($actual, $expected)) {
return;
}
throw new ExpectationFailedException(
Expand All @@ -310,7 +320,7 @@ public function assertNotInstanceOf(
*/
public function assertType(
string $expected,
$actual,
mixed $actual,
string $message = '',
): void {
if (is_type($expected)) {
Expand All @@ -336,7 +346,7 @@ public function assertType(
*/
public function assertNotType(
string $expected,
$actual,
mixed $actual,
string $message = '',
): void {
if (is_type($expected)) {
Expand All @@ -358,15 +368,12 @@ public function assertNotType(
}

public function assertContains(
$needle,
$haystack,
mixed $needle,
mixed $haystack,
string $message = '',
bool $ignoreCase = false,
): void {
if (
\is_array($haystack) ||
(\is_object($haystack) && $haystack instanceof Traversable)
) {
if ($haystack is Traversable<_>) {
if ((new Constraint\TraversableContains($needle))->matches($haystack)) {
return;
}
Expand Down Expand Up @@ -399,15 +406,12 @@ public function assertContains(
}

public function assertNotContains(
$needle,
$haystack,
mixed $needle,
mixed $haystack,
string $message = '',
bool $ignoreCase = false,
): void {
if (
\is_array($haystack) ||
(\is_object($haystack) && $haystack instanceof Traversable)
) {
if ($haystack is Traversable<_>) {
if (!(new Constraint\TraversableContains($needle))->matches($haystack)) {
return;
}
Expand Down Expand Up @@ -443,7 +447,7 @@ public function assertRegExp(
string $expected,
string $actual,
string $message = '',
) {
): void {
if (\preg_match($expected, $actual) === 1) {
return;
}
Expand All @@ -461,7 +465,7 @@ public function assertNotRegExp(
string $expected,
string $actual,
string $message = '',
) {
): void {
if (\preg_match($expected, $actual) === 0) {
return;
}
Expand All @@ -476,18 +480,18 @@ public function assertNotRegExp(
}

public function assertSubset(
$expected,
$actual,
dynamic $expected,
dynamic $actual,
string $msg = '',
string $path = '$actual',
): void {
foreach ($expected as $key => $value) {
if (is_any_array($actual)) {
if ($actual is KeyedContainer<_, _>) {
$actual_value = idx($actual, $key);
$part = '['.\var_export($key, true).']';
} else if (is_object($actual)) {
$actual_value = /* UNSAFE_EXPR */ $actual->$key;
$part = "->$key";
$part = "->".$key;
} else {
$actual_value = null;
$part = null;
Expand All @@ -496,7 +500,7 @@ public function assertSubset(
if (is_any_array($value) || is_object($value)) {
$this->assertSubset($value, $actual_value, $msg, $path.$part);
} else {
$this->assertEquals($value, $actual_value, $msg."\nKey: $path$part");
$this->assertEquals($value, $actual_value, $msg."\nKey: ".$path.$part);
}
}
}
Expand All @@ -506,12 +510,12 @@ public function assertSubset(
* that the contents of the two arrays are equal.
*/
public function assertKeyAndValueEquals(
array $expected,
array $actual,
KeyedContainer<mixed, mixed> $expected,
KeyedContainer<mixed, mixed> $actual,
string $msg = '',
): void {
self::sortArrayRecursive(&$expected);
self::sortArrayRecursive(&$actual);
$expected = self::sortArrayRecursive($expected);
$actual = self::sortArrayRecursive($actual);
$this->assertEquals($expected, $actual, $msg);
}

Expand Down Expand Up @@ -572,7 +576,7 @@ public function assertIsSorted<Tv>(
}

if (($pair->count() === 2) && !$comparator($pair[0], $pair[1])) {
$main_message = $message ?: 'Collection is not sorted';
$main_message = $message === '' ? 'Collection is not sorted' : $message;
$failure_detail = \sprintf(
'at pos %d, %s and %s are in the wrong order',
$index,
Expand Down Expand Up @@ -616,13 +620,17 @@ public function assertIsSortedByKey<Tv>(
);
}

private static function sortArrayRecursive(array &$arr): void {
foreach ($arr as $codemod_inserted_key => $i) {
if (is_array($i)) {
self::sortArrayRecursive(&$i);
private static function sortArrayRecursive(
KeyedContainer<mixed, mixed> $arr
): dict<arraykey, mixed> {
$out = dict[];
foreach ($arr as $k => $v) {
if ($v is KeyedContainer<_, _>) {
$v = self::sortArrayRecursive($v);
}
$arr[$codemod_inserted_key] = $i;
$out[$k as arraykey] = $v;
}
return $out;
}

private static function sorted<T>(Traversable<T> $x): ImmVector<T> {
Expand Down
6 changes: 3 additions & 3 deletions src/Constraint/TraversableContains.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?hh
<?hh // strict
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
Expand All @@ -13,9 +13,9 @@

class TraversableContains {

public function __construct(private $value) {}
public function __construct(private mixed $value) {}

public function matches($other): bool {
public function matches(Traversable<mixed> $other): bool {
if ($other instanceof \SplObjectStorage) {
return $other->contains($this->value);
}
Expand Down
Loading

0 comments on commit 2287da6

Please sign in to comment.