Skip to content

Commit 7be28cd

Browse files
authored
Replace unused Entity private method (#5029)
1 parent 82d39de commit 7be28cd

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

rector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878

7979
// private method called via getPrivateMethodInvoker
8080
RemoveUnusedPrivateMethodRector::class => [
81-
__DIR__ . '/system/Entity/Entity.php',
8281
__DIR__ . '/tests/system/Test/ReflectionHelperTest.php',
8382
],
8483

system/Entity/Entity.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -390,20 +390,6 @@ protected function castAs($value, string $attribute, string $method = 'get')
390390
return $handlers[$type]::$method($value, $params);
391391
}
392392

393-
/**
394-
* Cast as JSON
395-
*
396-
* @param mixed $value
397-
*
398-
* @throws CastException
399-
*
400-
* @return mixed
401-
*/
402-
private function castAsJson($value, bool $asArray = false)
403-
{
404-
return JsonCast::get($value, $asArray ? ['array'] : []);
405-
}
406-
407393
/**
408394
* Support for json_encode()
409395
*

tests/system/Entity/EntityTest.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Entity;
1313

14+
use Closure;
1415
use CodeIgniter\Entity\Exceptions\CastException;
1516
use CodeIgniter\HTTP\URI;
1617
use CodeIgniter\I18n\Time;
@@ -589,54 +590,60 @@ public function testCastAsJSONErrorUTF8()
589590

590591
public function testCastAsJSONSyntaxError()
591592
{
592-
$entity = new Entity();
593-
594-
$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
595-
596593
$this->expectException(CastException::class);
597594
$this->expectExceptionMessage('Syntax error, malformed JSON');
598595

599-
$method('{ this is bad string', true);
596+
(Closure::bind(static function (string $value) {
597+
$entity = new Entity();
598+
$entity->casts['dummy'] = 'json[array]';
599+
600+
return $entity->castAs($value, 'dummy');
601+
}, null, Entity::class))('{ this is bad string');
600602
}
601603

602604
public function testCastAsJSONAnotherErrorDepth()
603605
{
604-
$entity = new Entity();
605-
606-
$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
607-
608606
$this->expectException(CastException::class);
609607
$this->expectExceptionMessage('Maximum stack depth exceeded');
610608

611609
$string = '{' . str_repeat('"test":{', 513) . '"test":"value"' . str_repeat('}', 513) . '}';
612610

613-
$method($string, true);
611+
(Closure::bind(static function (string $value) {
612+
$entity = new Entity();
613+
$entity->casts['dummy'] = 'json[array]';
614+
615+
return $entity->castAs($value, 'dummy');
616+
}, null, Entity::class))($string);
614617
}
615618

616619
public function testCastAsJSONControlCharCheck()
617620
{
618-
$entity = new Entity();
619-
$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
620-
621621
$this->expectException(CastException::class);
622622
$this->expectExceptionMessage('Unexpected control character found');
623623

624624
$string = "{\n\t\"property1\": \"The quick brown fox\njumps over the lazy dog\",\n\t\"property2\":\"value2\"\n}";
625625

626-
$method($string, true);
626+
(Closure::bind(static function (string $value) {
627+
$entity = new Entity();
628+
$entity->casts['dummy'] = 'json[array]';
629+
630+
return $entity->castAs($value, 'dummy');
631+
}, null, Entity::class))($string);
627632
}
628633

629634
public function testCastAsJSONStateMismatch()
630635
{
631-
$entity = new Entity();
632-
$method = $this->getPrivateMethodInvoker($entity, 'castAsJson');
633-
634636
$this->expectException(CastException::class);
635637
$this->expectExceptionMessage('Underflow or the modes mismatch');
636638

637639
$string = '[{"name":"jack","product_id":"1234"]';
638640

639-
$method($string, true);
641+
(Closure::bind(static function (string $value) {
642+
$entity = new Entity();
643+
$entity->casts['dummy'] = 'json[array]';
644+
645+
return $entity->castAs($value, 'dummy');
646+
}, null, Entity::class))($string);
640647
}
641648

642649
public function testCastSetter()

0 commit comments

Comments
 (0)