diff --git a/CRM/Core/BAO/EntityTag.php b/CRM/Core/BAO/EntityTag.php index 580729a87c6f..3b23f84c0310 100644 --- a/CRM/Core/BAO/EntityTag.php +++ b/CRM/Core/BAO/EntityTag.php @@ -96,7 +96,7 @@ public static function dataExists($params) { * Delete the tag for a contact. * * @param array $params - * + * @deprecated * WARNING: Nonstandard params searches by tag_id rather than id! */ public static function del(&$params) { diff --git a/Civi/Api4/Generic/DAODeleteAction.php b/Civi/Api4/Generic/DAODeleteAction.php index b2b9584e921d..84d818de9b1c 100644 --- a/Civi/Api4/Generic/DAODeleteAction.php +++ b/Civi/Api4/Generic/DAODeleteAction.php @@ -14,6 +14,7 @@ use Civi\API\Exception\UnauthorizedException; use Civi\Api4\Utils\CoreUtil; +use Civi\Api4\Utils\ReflectionUtils; /** * Delete one or more $ENTITIES. @@ -56,7 +57,8 @@ protected function deleteObjects($items) { $ids = []; $baoName = $this->getBaoName(); - if ($this->getEntityName() !== 'EntityTag' && method_exists($baoName, 'del')) { + // Use BAO::del() method if it is not deprecated + if (method_exists($baoName, 'del') && !ReflectionUtils::isMethodDeprecated($baoName, 'del')) { foreach ($items as $item) { $args = [$item['id']]; $bao = call_user_func_array([$baoName, 'del'], $args); diff --git a/Civi/Api4/Utils/ReflectionUtils.php b/Civi/Api4/Utils/ReflectionUtils.php index a5d59803f13a..0263718e3aa4 100644 --- a/Civi/Api4/Utils/ReflectionUtils.php +++ b/Civi/Api4/Utils/ReflectionUtils.php @@ -179,6 +179,20 @@ public static function findStandardProperties($class): iterable { } } + /** + * Check if a class method is deprecated + * + * @param string $className + * @param string $methodName + * @return bool + * @throws \ReflectionException + */ + public static function isMethodDeprecated(string $className, string $methodName): bool { + $reflection = new \ReflectionClass($className); + $docBlock = $reflection->getMethod($methodName)->getDocComment(); + return strpos($docBlock, "@deprecated") !== FALSE; + } + /** * Find any methods in this class which match the given prefix. * diff --git a/tests/phpunit/api/v4/Mock/MockV4ReflectionGrandchild.php b/tests/phpunit/api/v4/Mock/MockV4ReflectionGrandchild.php index 6c0700f29531..5b2ebeeba6b6 100644 --- a/tests/phpunit/api/v4/Mock/MockV4ReflectionGrandchild.php +++ b/tests/phpunit/api/v4/Mock/MockV4ReflectionGrandchild.php @@ -31,4 +31,21 @@ */ class MockV4ReflectionGrandchild extends MockV4ReflectionChild { + /** + * Function marked deprecated + * @see \api\v4\Utils\ReflectionUtilsTest::testIsMethodDeprecated + * @deprecated + */ + public static function deprecatedFn() { + + } + + /** + * Function not marked deprecated + * @see \api\v4\Utils\ReflectionUtilsTest::testIsMethodDeprecated + */ + public static function nonDeprecatedFn() { + + } + } diff --git a/tests/phpunit/api/v4/Utils/ReflectionUtilsTest.php b/tests/phpunit/api/v4/Utils/ReflectionUtilsTest.php index 868ae79d640c..bd6d218a6d78 100644 --- a/tests/phpunit/api/v4/Utils/ReflectionUtilsTest.php +++ b/tests/phpunit/api/v4/Utils/ReflectionUtilsTest.php @@ -109,4 +109,10 @@ public function testParseDocBlock($input, $expected) { $this->assertEquals($expected, ReflectionUtils::parseDocBlock($input)); } + public function testIsMethodDeprecated() { + $mockClass = 'api\v4\Mock\MockV4ReflectionGrandchild'; + $this->assertTrue(ReflectionUtils::isMethodDeprecated($mockClass, 'deprecatedFn')); + $this->assertFalse(ReflectionUtils::isMethodDeprecated($mockClass, 'nonDeprecatedFn')); + } + }