Skip to content

Commit

Permalink
APIv4 - Use correct BAO delete function
Browse files Browse the repository at this point in the history
Uses BAO::del() only if it isn't deprecated.
  • Loading branch information
colemanw committed Aug 29, 2021
1 parent aafed2e commit 6196410
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/BAO/EntityTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion Civi/Api4/Generic/DAODeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Civi\API\Exception\UnauthorizedException;
use Civi\Api4\Utils\CoreUtil;
use Civi\Api4\Utils\ReflectionUtils;

/**
* Delete one or more $ENTITIES.
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions Civi/Api4/Utils/ReflectionUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/phpunit/api/v4/Mock/MockV4ReflectionGrandchild.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

}

}
6 changes: 6 additions & 0 deletions tests/phpunit/api/v4/Utils/ReflectionUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}

0 comments on commit 6196410

Please sign in to comment.