From b5533e468012cfae1857c400da2a7140036ab67d Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 28 Nov 2022 19:16:31 +1300 Subject: [PATCH] API Stop using deprecated API --- src/Core/Convert.php | 4 +- src/Dev/CsvBulkLoader.php | 8 +++- src/Dev/Deprecation.php | 7 +++ src/Dev/SapphireTest.php | 9 +++- src/Security/Member.php | 4 +- src/View/Requirements_Backend.php | 44 ++++++++++++------- src/View/SSViewer.php | 2 +- tests/php/Core/ConvertTest.php | 10 ++++- tests/php/Core/ObjectTest.php | 8 ++-- tests/php/Logging/MonologErrorHandlerTest.php | 9 ++-- tests/php/View/RequirementsTest.php | 5 ++- tests/php/View/SSViewerTest.php | 3 ++ 12 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/Core/Convert.php b/src/Core/Convert.php index 2021e8446..f3ac4d271 100644 --- a/src/Core/Convert.php +++ b/src/Core/Convert.php @@ -281,7 +281,7 @@ class Convert */ public static function json2array($val) { - Deprecation::notice('4.4.0', 'Use json_decode() instead'); + Deprecation::notice('4.4.0', 'Use json_decode($val, true) instead'); return json_decode($val ?? '', true); } @@ -331,9 +331,11 @@ class Convert * @param SimpleXMLElement $xml * * @return mixed + * @deprecated 4.11.0 Will be removed without equivalent functionality */ protected static function recursiveXMLToArray($xml) { + Deprecation::notice('4.11.0', 'Will be removed without equivalent functionality'); $x = null; if ($xml instanceof SimpleXMLElement) { $attributes = $xml->attributes(); diff --git a/src/Dev/CsvBulkLoader.php b/src/Dev/CsvBulkLoader.php index d7beb7103..a4384078b 100644 --- a/src/Dev/CsvBulkLoader.php +++ b/src/Dev/CsvBulkLoader.php @@ -183,7 +183,9 @@ class CsvBulkLoader extends BulkLoader $lines = $this->config()->get("lines"); } - $new = $this->getNewSplitFileName(); + $new = Deprecation::withNoReplacement(function () { + return $this->getNewSplitFileName(); + }); $to = fopen($new ?? '', 'w+'); $from = fopen($path ?? '', 'r'); @@ -209,7 +211,9 @@ class CsvBulkLoader extends BulkLoader fclose($to); // get a new temporary file name, to write the next lines to - $new = $this->getNewSplitFileName(); + $new = Deprecation::withNoReplacement(function () { + return $this->getNewSplitFileName(); + }); $to = fopen($new ?? '', 'w+'); diff --git a/src/Dev/Deprecation.php b/src/Dev/Deprecation.php index 3ca6a0d3b..fff3e8971 100644 --- a/src/Dev/Deprecation.php +++ b/src/Dev/Deprecation.php @@ -228,16 +228,23 @@ class Deprecation if (!self::isEnabled()) { return; } + $outputMessages = []; // using a while loop with array_shift() to ensure that self::$userErrorMessageBuffer will have // have values removed from it before calling user_error() while (count(self::$userErrorMessageBuffer)) { $arr = array_shift(self::$userErrorMessageBuffer); $message = $arr['message']; + // often the same deprecation message appears dozens of times, which isn't helpful + // only need to show a single instance of each message + if (in_array($message, $outputMessages)) { + continue; + } $calledInsideWithNoReplacement = $arr['calledInsideWithNoReplacement']; if ($calledInsideWithNoReplacement && !self::$showNoReplacementNotices) { continue; } user_error($message, E_USER_DEPRECATED); + $outputMessages[] = $message; } } diff --git a/src/Dev/SapphireTest.php b/src/Dev/SapphireTest.php index 4440a00c0..17a1a1404 100644 --- a/src/Dev/SapphireTest.php +++ b/src/Dev/SapphireTest.php @@ -49,6 +49,7 @@ use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; use SilverStripe\View\SSViewer; +use SilverStripe\Dev\Deprecation; /* ------------------------------------------------- * @@ -568,7 +569,9 @@ if (class_exists(IsEqualCanonicalizing::class)) { { Deprecation::notice('4.0.1', 'Use FixtureTestState instead'); $fixture = Injector::inst()->create(YamlFixture::class, $fixtureFile); - $fixture->writeInto($this->getFixtureFactory()); + Deprecation::withNoReplacement(function () use ($fixture) { + $fixture->writeInto($this->getFixtureFactory()); + }); } /** @@ -1008,7 +1011,9 @@ if (class_exists(IsEqualCanonicalizing::class)) { $kernel = new TestKernel(BASE_PATH); // PHPUnit 9 only logic to exclude old test still targeting PHPUNit 5.7 - $kernel->setIgnoredCIConfigs([Module::CI_PHPUNIT_FIVE, Module::CI_UNKNOWN]); + Deprecation::withNoReplacement(function () use ($kernel) { + $kernel->setIgnoredCIConfigs([Module::CI_PHPUNIT_FIVE, Module::CI_UNKNOWN]); + }); if (class_exists(HTTPApplication::class)) { // Mock request diff --git a/src/Security/Member.php b/src/Security/Member.php index 6fb7a2f36..869b82efd 100644 --- a/src/Security/Member.php +++ b/src/Security/Member.php @@ -834,7 +834,9 @@ class Member extends DataObject public static function create_new_password() { Deprecation::notice('4.12.0', 'Will be removed without equivalent functionality to replace it'); - $words = Security::config()->uninherited('word_list'); + $words = Deprecation::withNoReplacement(function () { + return Security::config()->uninherited('word_list'); + }); if ($words && file_exists($words ?? '')) { $words = file($words ?? ''); diff --git a/src/View/Requirements_Backend.php b/src/View/Requirements_Backend.php index 3052d7b0a..e6aed80c8 100644 --- a/src/View/Requirements_Backend.php +++ b/src/View/Requirements_Backend.php @@ -143,6 +143,7 @@ class Requirements_Backend * Use the injected minification service to minify any javascript file passed to {@link combine_files()}. * * @var bool + * @deprecated 4.0.1 Will be removed without equivalent functionality */ protected $minifyCombinedFiles = false; @@ -212,6 +213,7 @@ class Requirements_Backend /** * @var Requirements_Minifier + * @deprecated 4.0.1 Will be removed without equivalent functionality */ protected $minifier = null; @@ -251,9 +253,11 @@ class Requirements_Backend * Set a new minification service for this backend * * @param Requirements_Minifier $minifier + * @deprecated 4.0.1 Will be removed without equivalent functionality */ public function setMinifier(Requirements_Minifier $minifier = null) { + Deprecation::notice('4.0.1', 'Will be removed without equivalent functionality'); $this->minifier = $minifier; } @@ -389,9 +393,11 @@ class Requirements_Backend * Check if minify files should be combined * * @return bool + * @deprecated 4.0.1 Will be removed without equivalent functionality */ public function getMinifyCombinedFiles() { + Deprecation::notice('4.0.1', 'Will be removed without equivalent functionality'); return $this->minifyCombinedFiles; } @@ -400,9 +406,11 @@ class Requirements_Backend * * @param bool $minify * @return $this + * @deprecated 4.0.1 Will be removed without equivalent functionality */ public function setMinifyCombinedFiles($minify) { + Deprecation::notice('4.0.1', 'Will be removed without equivalent functionality'); $this->minifyCombinedFiles = $minify; return $this; } @@ -1381,22 +1389,25 @@ class Requirements_Backend $combinedFileID = File::join_paths($this->getCombinedFilesFolder(), $combinedFile); // Send file combination request to the backend, with an optional callback to perform regeneration - $minify = $this->getMinifyCombinedFiles(); - if ($minify && !$this->minifier) { - throw new Exception( - sprintf( - <<getMinifyCombinedFiles(); + if ($minify && !$this->minifier) { + throw new Exception( + sprintf( + <<getAssetHandler() @@ -1416,6 +1427,7 @@ MESSAGE $fileContent = $this->resolveCSSReferences($fileContent, $file); } // Use configured minifier + // @deprecated if ($minify) { $fileContent = $this->minifier->minify($fileContent, $type, $file); } diff --git a/src/View/SSViewer.php b/src/View/SSViewer.php index 2057f5974..163f82273 100644 --- a/src/View/SSViewer.php +++ b/src/View/SSViewer.php @@ -74,7 +74,7 @@ class SSViewer implements Flushable * The used "theme", which usually consists of templates, images and stylesheets. * Only used when {@link $theme_enabled} is set to TRUE, and $themes is empty * - * @deprecated 4.0.0:5.0.0 + * @deprecated 4.0.0 Use themes config instead * @config * @var string */ diff --git a/tests/php/Core/ConvertTest.php b/tests/php/Core/ConvertTest.php index 1c846f974..09546feab 100644 --- a/tests/php/Core/ConvertTest.php +++ b/tests/php/Core/ConvertTest.php @@ -419,7 +419,9 @@ PHP */ public function testXML2Array() { - + if (Deprecation::isEnabled()) { + $this->markTestSkipped('Test calls deprecated code'); + } $inputXML = << markTestSkipped('Test calls deprecated code'); + } $inputXML = << markTestSkipped('Test calls deprecated code'); + } $inputXML = << ]> diff --git a/tests/php/Core/ObjectTest.php b/tests/php/Core/ObjectTest.php index 66562315a..76b994b14 100644 --- a/tests/php/Core/ObjectTest.php +++ b/tests/php/Core/ObjectTest.php @@ -117,7 +117,7 @@ class ObjectTest extends SapphireTest $obj = singleton(MyObject::class); $this->assertEquals( 'MyObject', - $obj->stat('mystaticProperty'), + $obj::config()->get('mystaticProperty'), 'Uninherited statics through stat() on a singleton behave the same as built-in PHP statics' ); } @@ -129,7 +129,7 @@ class ObjectTest extends SapphireTest } $subObj = singleton(MyObject::class); $this->assertEquals( - $subObj->stat('mystaticProperty'), + $subObj::config()->get('mystaticProperty'), 'MyObject', 'Statics defined on a parent class are available through stat() on a subclass' ); @@ -144,7 +144,7 @@ class ObjectTest extends SapphireTest $singleton2 = singleton(MyObject::class); $singleton1->set_stat('mystaticProperty', 'changed'); $this->assertEquals( - $singleton2->stat('mystaticProperty'), + $singleton2::config()->get('mystaticProperty'), 'changed', 'Statics setting is populated throughout singletons without explicitly clearing cache' ); @@ -159,7 +159,7 @@ class ObjectTest extends SapphireTest $instance2 = new ObjectTest\MyObject(); $instance1->set_stat('mystaticProperty', 'changed'); $this->assertEquals( - $instance2->stat('mystaticProperty'), + $instance2::config()->get('mystaticProperty'), 'changed', 'Statics setting through set_stat() is populated throughout instances without explicitly clearing cache' ); diff --git a/tests/php/Logging/MonologErrorHandlerTest.php b/tests/php/Logging/MonologErrorHandlerTest.php index b31251e91..e5473b548 100644 --- a/tests/php/Logging/MonologErrorHandlerTest.php +++ b/tests/php/Logging/MonologErrorHandlerTest.php @@ -20,6 +20,9 @@ class MonologErrorHandlerTest extends SapphireTest public function testSetLoggerResetsStack() { + if (Deprecation::isEnabled()) { + $this->markTestSkipped('Test calls deprecated code'); + } /** @var LoggerInterface $logger */ $logger = $this->createMock(LoggerInterface::class); @@ -27,10 +30,8 @@ class MonologErrorHandlerTest extends SapphireTest $handler->pushLogger($logger)->pushLogger($logger); $this->assertCount(2, $handler->getLoggers(), 'Loggers are pushed to the stack'); - if (!Deprecation::isEnabled()) { - $handler->setLogger($logger); - $this->assertCount(1, $handler->getLoggers(), 'setLogger resets stack and pushes'); - } + $handler->setLogger($logger); + $this->assertCount(1, $handler->getLoggers(), 'setLogger resets stack and pushes'); $handler->setLoggers([]); $this->assertCount(0, $handler->getLoggers(), 'setLoggers overwrites all configured loggers'); diff --git a/tests/php/View/RequirementsTest.php b/tests/php/View/RequirementsTest.php index ae9d19558..8b028a654 100644 --- a/tests/php/View/RequirementsTest.php +++ b/tests/php/View/RequirementsTest.php @@ -14,6 +14,7 @@ use SilverStripe\View\Requirements_Backend; use SilverStripe\Core\Manifest\ResourceURLGenerator; use SilverStripe\Control\SimpleResourceURLGenerator; use SilverStripe\Core\Config\Config; +use SilverStripe\Dev\Deprecation; use SilverStripe\View\SSViewer; use SilverStripe\View\ThemeResourceLoader; @@ -302,7 +303,9 @@ class RequirementsTest extends SapphireTest $backend->clear(); $backend->clearCombinedFiles(); $backend->setCombinedFilesFolder('_combinedfiles'); - $backend->setMinifyCombinedFiles(false); + Deprecation::withNoReplacement(function () use ($backend) { + $backend->setMinifyCombinedFiles(false); + }); $backend->setCombinedFilesEnabled(true); Requirements::flush(); } diff --git a/tests/php/View/SSViewerTest.php b/tests/php/View/SSViewerTest.php index ac3779942..1e9ee3676 100644 --- a/tests/php/View/SSViewerTest.php +++ b/tests/php/View/SSViewerTest.php @@ -277,6 +277,9 @@ class SSViewerTest extends SapphireTest public function testRequirementsMinification() { + if (Deprecation::isEnabled()) { + $this->markTestSkipped('Test calls deprecated code'); + } /** @var Requirements_Backend $testBackend */ $testBackend = Injector::inst()->create(Requirements_Backend::class); $testBackend->setSuffixRequirements(false);