Skip to content

Commit

Permalink
[TypeDeclaration] Convert inline @var tag to assert()
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-granados committed Sep 11, 2024
1 parent 71fb2e4 commit 96dbd95
Show file tree
Hide file tree
Showing 24 changed files with 800 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector;

return RectorConfig::configure()
->withPreparedSets(
Expand Down Expand Up @@ -57,4 +58,5 @@
],

RemoveUnusedPrivatePropertyRector::class => [__DIR__ . '/src/Configuration/RectorConfigBuilder.php'],
InlineVarDocTagToAssertRector::class,
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/** @var false $foo */
$foo = getFalse();

/** @var true $foo */
$foo = getTrue();

/** @var string $foo */
$foo = getString();

/** @var float $foo */
$foo = getFloat();

/** @var int $foo */
$foo = getInt();

/** @var bool $foo */
$foo = getBool();

/** @var null $foo */
$foo = getNull();

/** @var callable $foo */
$foo = getCallable();

/** @var object $foo */
$foo = getObject();

/** @var array $foo */
$foo = getArray();

/** @var iterable $foo */
$foo = getIterable();

?>
-----
<?php

$foo = getFalse();
assert($foo === false);

$foo = getTrue();
assert($foo === true);

$foo = getString();
assert(is_string($foo));

$foo = getFloat();
assert(is_float($foo));

$foo = getInt();
assert(is_int($foo));

$foo = getBool();
assert(is_bool($foo));

$foo = getNull();
assert($foo === null);

$foo = getCallable();
assert(is_callable($foo));

$foo = getObject();
assert(is_object($foo));

$foo = getArray();
assert(is_array($foo));

$foo = getIterable();
assert(is_iterable($foo));

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

use Foo\Bar;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\FooInterface;
use Traversable;

/** @var Bar $foo */
$foo = getBar();

/** @var \Foo\Bar $foo */
$foo = getFooBar();

/** @var FooInterface $foo */
$foo = getFooInterface();

/** @var Traversable $foo */
$foo = getTraversable();

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

use Foo\Bar;
use Rector\Tests\Renaming\Rector\MethodCall\RenameMethodRector\Source\FooInterface;
use Traversable;

$foo = getBar();
assert($foo instanceof Bar);

$foo = getFooBar();
assert($foo instanceof \Foo\Bar);

$foo = getFooInterface();
assert($foo instanceof FooInterface);

$foo = getTraversable();
assert($foo instanceof Traversable);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/** @var Foo&Bar $foo */
$foo = getFooAndBar();

/** @var \Foo&\Bar $foo */
$foo = getFooAndBar();

?>
-----
<?php

$foo = getFooAndBar();
assert($foo instanceof Foo && $foo instanceof Bar);

$foo = getFooAndBar();
assert($foo instanceof \Foo && $foo instanceof \Bar);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/** @var ?string $foo */
$foo = getNullableString();

/** @var ?Foo $foo */
$foo = getNullableFoo();

?>
-----
<?php

$foo = getNullableString();
assert(is_string($foo) || $foo === null);

$foo = getNullableFoo();
assert($foo instanceof Foo || $foo === null);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

class OtherClass {}

class SomeClass extends OtherClass
{
function test()
{
/** @var self $foo */
$foo = getSelf();

/** @var static $foo */
$foo = getStatic();

/** @var parent $foo */
$foo = getParent();
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

class OtherClass {}

class SomeClass extends OtherClass
{
function test()
{
$foo = getSelf();
assert($foo instanceof self);

$foo = getStatic();
assert($foo instanceof static);

$foo = getParent();
assert($foo instanceof parent);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/** @var string|false $foo */
$foo = getStringOrFalse();

/** @var string|int|null $foo */
$foo = getStringOrFalse();

/** @var int|float|bool|string|array $foo */
$foo = getMultipleTypes();

/** @var string|Foo $foo */
$foo = getStringOrFoo();

/** @var Foo|Bar $foo */
$foo = getFooOrBar();

/** @var \Foo|\Bar $foo */
$foo = getFooOrBar();

?>
-----
<?php

$foo = getStringOrFalse();
assert(is_string($foo) || $foo === false);

$foo = getStringOrFalse();
assert(is_string($foo) || is_int($foo) || $foo === null);

$foo = getMultipleTypes();
assert(is_int($foo) || is_float($foo) || is_bool($foo) || is_string($foo) || is_array($foo));

$foo = getStringOrFoo();
assert(is_string($foo) || $foo instanceof Foo);

$foo = getFooOrBar();
assert($foo instanceof Foo || $foo instanceof Bar);

$foo = getFooOrBar();
assert($foo instanceof \Foo || $foo instanceof \Bar);

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

function test() {
/** @var string $foo */
$foo = getString();
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

function test() {
$foo = getString();
assert(is_string($foo));
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

final class SomeClass
{
function test() {
/** @var string $foo */
$foo = getString();
}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Expression\InlineVarDocTagToAssertRector\Fixture;

final class SomeClass
{
function test() {
$foo = getString();
assert(is_string($foo));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @var string $foo
* This is the string
*/
$foo = getString();

/**
* We are going to do something
* @var string $foo
*/
$foo = getString();

/**
* @var string $foo
* @deprecated
*/
$foo = getString();

/**
* @var string $foo
* @see getStringTest()
* Please do not change
*/
$foo = getString();

/**
* @var string $foo
* @var int $bar
*/
$foo = getString();
$bar = getInt();

/** @var string $foo this is the string */
$foo = getString();

?>
-----
<?php

/**
* @var string $foo
* This is the string
*/
$foo = getString();

/**
* We are going to do something
*/
$foo = getString();
assert(is_string($foo));

/**
* @deprecated
*/
$foo = getString();
assert(is_string($foo));

/**
* @see getStringTest()
* Please do not change
*/
$foo = getString();
assert(is_string($foo));

/**
* @var int $bar
*/
$foo = getString();
assert(is_string($foo));
$bar = getInt();

/** @var string $foo this is the string */
$foo = getString();

?>
Loading

0 comments on commit 96dbd95

Please sign in to comment.