Skip to content

Commit

Permalink
[Php80] Add Php8ResourceReturnToObjectRector (#1068)
Browse files Browse the repository at this point in the history
* [Php80] Add Php8ResourceToObjectRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* apply with check is_resource

* [ci-review] Rector Rectify

* ensure assign var is the arg resource

* applied for curl_init() return

* phpstan

* register curl_multi_init and curl_share_init

* skip double check

* ensure variable compare equal on double check

* phpstan

* rename to Php8ResourceReturnToObjectRector

* rename test

* rename service

* check via NodeTypeResolver->getType()

* [ci-review] Rector Rectify

* check double check specifically for BooleanOr

* handle double check

* skip complex condition

* skip complex condition

* refactoring

* refactoring

* refactoring

* [ci-review] Rector Rectify

* handle flip check

* phpstan

* handle combined with ChangeOrIfReturnToEarlyReturnRector

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* typo fix

* add socket_create example

* more list for socket

* remove comment

* check different instanceof check

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* add failing fixture for different condition

* fix

* phpstan

* phpstan

* register xmlwriter_open_memory

* add XMLParser

* more to the list

* add example for openssl

* example for shmop_open

* add example for msg_get_queue

* add example for inflate_init

* add example for enchant_broker_init

* register GD functions

* add example for imagecreate

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user authored Oct 27, 2021
1 parent 7b848a7 commit 6bcdc92
Show file tree
Hide file tree
Showing 29 changed files with 1,121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/set/php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\Php80\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
use Rector\Php80\Rector\ClassMethod\SetStateToStaticRector;
use Rector\Php80\Rector\FuncCall\ClassOnObjectRector;
use Rector\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector;
use Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector;
use Rector\Php80\Rector\FunctionLike\UnionTypesRector;
use Rector\Php80\Rector\Identical\StrEndsWithRector;
Expand Down Expand Up @@ -108,4 +109,6 @@
new ReplaceFuncCallArgumentDefaultValue('version_compare', 2, 'n', 'ne'),
]),
]]);

$services->set(Php8ResourceReturnToObjectRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCondition
{
public function run()
{
$ch = curl_init();
is_resource($ch) || rand(0, 1);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCondition
{
public function run()
{
$ch = curl_init();
$ch instanceof \CurlHandle || rand(0, 1);
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DifferentInstanceof
{
public function run()
{
$ch = curl_init();
is_resource($ch) || $ch instanceof \stdClass;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DifferentInstanceof
{
public function run()
{
$ch = curl_init();
$ch instanceof \CurlHandle || $ch instanceof \stdClass;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DifferentInstanceofVariable
{
public function run($abc)
{
$ch = curl_init();
is_resource($ch) || $abc instanceof \stdClass;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DifferentInstanceofVariable
{
public function run($abc)
{
$ch = curl_init();
$ch instanceof \CurlHandle || $abc instanceof \stdClass;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCheck
{
public function run()
{
$ch = curl_init();
is_resource($ch) || $ch instanceof \CurlHandle;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCheck
{
public function run()
{
$ch = curl_init();
$ch instanceof \CurlHandle;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCheckFlip
{
public function run()
{
$ch = curl_init();
$ch instanceof \CurlHandle || is_resource($ch);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCheckFlip
{
public function run()
{
$ch = curl_init();
$ch instanceof \CurlHandle;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCheckWithParenthesis
{
public function run()
{
$ch = curl_init();
file_exists($fileName) && (is_resource($ch) || $ch instanceof \CurlHandle) && is_dir($dir);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class DoubleCheckWithParenthesis
{
public function run()
{
$ch = curl_init();
file_exists($fileName) && ($ch instanceof \CurlHandle) && is_dir($dir);
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SkipComplexCondition
{
public function run(string $fileName, string $dir)
{
$ch = curl_init();
file_exists($fileName) && is_resource($ch) || $ch instanceof \CurlHandle && is_dir($dir);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeMsgGetQueue
{
public function run()
{
$seg = msg_get_queue($MSGKey) ;
is_resource($seg);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeMsgGetQueue
{
public function run()
{
$seg = msg_get_queue($MSGKey) ;
$seg instanceof \SysvMessageQueue;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeCurlInit
{
public function run()
{
$ch = curl_init();
is_resource($ch);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeCurlInit
{
public function run()
{
$ch = curl_init();
$ch instanceof \CurlHandle;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeEnhanceBrokerInit
{
public function run()
{
$ch = enchant_broker_init();
is_resource($ch);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeEnhanceBrokerInit
{
public function run()
{
$ch = enchant_broker_init();
$ch instanceof \EnchantBroker;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeImageCreate
{
public function run()
{
$gd = imagecreate(1, 2);
is_resource($gd);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeImageCreate
{
public function run()
{
$gd = imagecreate(1, 2);
$gd instanceof \GdImage;
}
}

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

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeInflateInit
{
public function run()
{
$context = inflate_init(ZLIB_ENCODING_DEFLATE);
is_resource($context);
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\FuncCall\Php8ResourceReturnToObjectRector\Fixture;

final class SomeInflateInit
{
public function run()
{
$context = inflate_init(ZLIB_ENCODING_DEFLATE);
$context instanceof \InflateContext;
}
}

?>
Loading

0 comments on commit 6bcdc92

Please sign in to comment.