Skip to content

Commit

Permalink
Support native "mixed" class properties in PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
jorrit authored and cweiske committed Jan 27, 2024
1 parent ffc08ba commit 59e2c2a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ protected function getFullNamespace($type, $strNs)
return $type;
}
list($first) = explode('[', $type, 2);
if ($first === 'mixed' || $this->isSimpleType($first)) {
if ($this->isSimpleType($first)) {
return $type;
}

Expand Down Expand Up @@ -734,7 +734,8 @@ protected function isSimpleType($type)
|| $type == 'boolean' || $type == 'bool'
|| $type == 'integer' || $type == 'int'
|| $type == 'double' || $type == 'float'
|| $type == 'array' || $type == 'object';
|| $type == 'array' || $type == 'object'
|| $type === 'mixed';
}

/**
Expand All @@ -756,7 +757,7 @@ protected function isObjectOfSameType($type, $value)

/**
* Checks if the given type is a type that is not nested
* (simple type except array and object)
* (simple type except array, object and mixed)
*
* @param string $type type name from gettype()
*
Expand Down
60 changes: 60 additions & 0 deletions tests/MixedType_PHP80_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Unit tests for JsonMapper's support for PHP 8.0 mixed type
*
* @category Tools
* @package JsonMapper
* @author Lukas Cerny <[email protected]>
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link https://github.com/cweiske/jsonmapper
* @requires PHP 8.0
*/
class MixedType_PHP80_Test extends \PHPUnit\Framework\TestCase
{
const TEST_DATA_COMPLEX = '{"data": { "id": 123, "name": "Test User" }}';
const TEST_DATA_SIMPLE = '{"data": 123}';

/**
* Sets up test cases loading required classes.
*
* This is in setUp and not at the top of this file to ensure this is only
* executed with PHP 8.0 (due to the `@requires` tag).
*/
protected function setUp(): void
{
require_once 'namespacetest/PhpMixedType.php';
}

/**
* Test for PHP 8.0 mixed type containing an object.
*/
public function testStrictTypesMapping_ComplexValue()
{
$jm = new JsonMapper();
/** @var \namespacetest\PhpMixedType $sn */
$sn = $jm->map(
json_decode(self::TEST_DATA_COMPLEX),
new \namespacetest\PhpMixedType()
);

$this->assertInstanceOf(stdClass::class, $sn->data);
$this->assertEquals(123, $sn->data->id);
$this->assertEquals('Test User', $sn->data->name);
}

/**
* Test for PHP 8.0 mixed type containing an int.
*/
public function testStrictTypesMapping_SimpleValue()
{
$jm = new JsonMapper();
/** @var \namespacetest\PhpMixedType $sn */
$sn = $jm->map(
json_decode(self::TEST_DATA_SIMPLE),
new \namespacetest\PhpMixedType()
);

$this->assertEquals(123, $sn->data);
}
}
10 changes: 10 additions & 0 deletions tests/namespacetest/PhpMixedType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace namespacetest;

class PhpMixedType
{
public mixed $data;
}

0 comments on commit 59e2c2a

Please sign in to comment.