Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ProxyGenerator): update regex pattern to match simple id getters with custom id type #1001

Open
wants to merge 1 commit into
base: 3.4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,16 @@ parameters:
-
message: '#^Result of method Doctrine\\Tests\\Common\\Proxy\\LazyLoadableObjectWithVoid::(adding|incrementing)AndReturningVoid\(\) \(void\) is used\.$#'
path: 'tests/Common/Proxy/ProxyLogicVoidReturnTypeTest.php'
-
message: '#^Property Doctrine\\Tests\\Common\\Proxy\\ProxyLogicTest::\$initializerCallbackMock \(callable\(\): mixed&PHPUnit\\Framework\\MockObject\\MockObject\) does not accept PHPUnit\\Framework\\MockObject\\MockObject&stdClass\.$#'
path: 'tests/Common/Proxy/ProxyLogicTest.php'
-
message: '#.*LazyLoadableObject.*#'
paths:
- 'tests/Common/Proxy/ProxyLogicTest.php'
- 'tests/Common/Proxy/ProxyLogicVoidReturnTypeTest.php'
-
message: '#^Instantiated class Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\.* not found.$#'
path: 'tests/Common/Proxy/ProxyLogicTest.php'
-
message: '#^Instantiated class Doctrine\\Tests\\Common\\ProxyProxy\\__CG__\\Doctrine\\Tests\\Common\\Proxy\\.* not found.$#'
path: 'tests/Common/Proxy/ProxyLogicVoidReturnTypeTest.php'
-
message: '#^Property Doctrine\\Tests\\Common\\Proxy\\ProxyLogicVoidReturnTypeTest::\$initializerCallbackMock \(callable\(\): mixed&PHPUnit\\Framework\\MockObject\\MockObject\) does not accept PHPUnit\\Framework\\MockObject\\MockObject&stdClass\.$#'
path: 'tests/Common/Proxy/ProxyLogicVoidReturnTypeTest.php'
-
message: '#^Method Doctrine\\Tests\\Common\\Proxy\\MagicIssetClassWithInteger::__isset\(\) should return bool but returns int\.$#'
path: 'tests/Common/Proxy/MagicIssetClassWithInteger.php'
Expand Down
2 changes: 1 addition & 1 deletion src/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ProxyGenerator
(?<type>\\?[a-z_\x7f-\xff][\w\x7f-\xff]*(?:\\[a-z_\x7f-\xff][\w\x7f-\xff]*)*)
(?<intersection_type>(?&type)\s*&\s*(?&type))
(?<union_type>(?:(?:\(\s*(?&intersection_type)\s*\))|(?&type))(?:\s*\|\s*(?:(?:\(\s*(?&intersection_type)\s*\))|(?&type)))+)
)(?:public\s+)?(?:function\s+%s\s*\(\)\s*)\s*(?::\s*(?:(?&union_type)|(?&intersection_type)|(?:\??(?&type)))\s*)?{\s*return\s*\$this->%s;\s*})i
)(?:public\s+)?(?:function\s+%1$s\s*\(\)\s*)\s*(?::\s*(?:(?&union_type)|(?&intersection_type)|(?:\??(?&type)))\s*)?{\s*return\s*(\$this->%2$s|new\s+(?&type)\(\$this->%2$s\)|(?&type)::\w+\(\$this->%2$s\));\s*})i
EOT;

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Common/Proxy/LazyLoadableObjectWithCustomIdType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Doctrine\Tests\Common\Proxy;

use Doctrine;

/**
* Test asset representing a lazy loadable object
*/
class LazyLoadableObjectWithCustomIdType
{
/** @var string */
private $identifierFieldWithStaticVOConstructor;

/** @var string */
private $identifierFieldWithVOConstructor;

public function getIdentifierFieldWithStaticVOConstructor() : ValueId
{
return ValueId::new($this->identifierFieldWithStaticVOConstructor);
}

public function getIdentifierFieldWithVOConstructor() : ValueId
{
return new ValueId($this->identifierFieldWithVOConstructor);
}
}
164 changes: 164 additions & 0 deletions tests/Common/Proxy/LazyLoadableObjectWithCustomIdTypeClassMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php
namespace Doctrine\Tests\Common\Proxy;

use Doctrine\Persistence\Mapping\ClassMetadata;
use ReflectionClass;

/**
* Class metadata test asset for @see LazyLoadableObject
*/
class LazyLoadableObjectWithCustomIdTypeClassMetadata implements ClassMetadata
{
/**
* @var ReflectionClass
*/
protected $reflectionClass;

/**
* @var array
*/
protected $identifier = [
'identifierFieldWithStaticVOConstructor' => true,
'identifierFieldWithVOConstructor' => true,
];

/**
* @var array
*/
protected $fields = [
'identifierFieldWithStaticVOConstructor' => true,
'identifierFieldWithVOConstructor' => true,
];

/**
* {@inheritDoc}
*/
public function getName()
{
return $this->getReflectionClass()->getName();
}

/**
* {@inheritDoc}
*/
public function getIdentifier()
{
return array_keys($this->identifier);
}

/**
* {@inheritDoc}
*/
public function getReflectionClass()
{
if (null === $this->reflectionClass) {
$this->reflectionClass = new \ReflectionClass(__NAMESPACE__ . '\LazyLoadableObjectWithCustomIdType');
}

return $this->reflectionClass;
}

/**
* {@inheritDoc}
*/
public function isIdentifier($fieldName)
{
return isset($this->identifier[$fieldName]);
}

/**
* {@inheritDoc}
*/
public function hasField($fieldName)
{
return isset($this->fields[$fieldName]);
}

/**
* {@inheritDoc}
*/
public function hasAssociation($fieldName)
{
return false;
}

/**
* {@inheritDoc}
*/
public function isSingleValuedAssociation($fieldName)
{
throw new \BadMethodCallException('not implemented');
}

/**
* {@inheritDoc}
*/
public function isCollectionValuedAssociation($fieldName)
{
throw new \BadMethodCallException('not implemented');
}

/**
* {@inheritDoc}
*/
public function getFieldNames()
{
return array_keys($this->fields);
}

/**
* {@inheritDoc}
*/
public function getIdentifierFieldNames()
{
return $this->getIdentifier();
}

/**
* {@inheritDoc}
*/
public function getAssociationNames()
{
return [];
}

/**
* {@inheritDoc}
*/
public function getTypeOfField($fieldName)
{
return 'string';
}

/**
* {@inheritDoc}
*/
public function getAssociationTargetClass($assocName)
{
throw new \BadMethodCallException('not implemented');
}

/**
* {@inheritDoc}
*/
public function isAssociationInverseSide($assocName)
{
throw new \BadMethodCallException('not implemented');
}

/**
* {@inheritDoc}
*/
public function getAssociationMappedByTargetField($assocName)
{
throw new \BadMethodCallException('not implemented');
}

/**
* {@inheritDoc}
*/
public function getIdentifierValues($object)
{
throw new \BadMethodCallException('not implemented');
}
}
19 changes: 19 additions & 0 deletions tests/Common/Proxy/LazyLoadableObjectWithNoGetPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Doctrine\Tests\Common\Proxy;

use Doctrine;

/**
* Test asset representing a lazy loadable object
*/
class LazyLoadableObjectWithNoGetPrefix
{
/** @var string */
private $identifierField;

public function identifierField() : ValueId
{
return new ValueId($this->identifierField);
}

}
Loading