Skip to content

Commit

Permalink
API Stop using deprecated API
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 28, 2022
1 parent 2058293 commit b5533e4
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/Core/Convert.php
Expand Up @@ -281,7 +281,7 @@ public static function json2obj($val)
*/
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);
}
Expand Down Expand Up @@ -331,9 +331,11 @@ public static function xml2array($val, $disableDoctypes = false, $disableExterna
* @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();
Expand Down
8 changes: 6 additions & 2 deletions src/Dev/CsvBulkLoader.php
Expand Up @@ -183,7 +183,9 @@ protected function splitFile($path, $lines = null)
$lines = $this->config()->get("lines");
}

$new = $this->getNewSplitFileName();
$new = Deprecation::withNoReplacement(function () {
return $this->getNewSplitFileName();
});

$to = fopen($new ?? '', 'w+');
$from = fopen($path ?? '', 'r');
Expand All @@ -209,7 +211,9 @@ protected function splitFile($path, $lines = null)
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+');

Expand Down
7 changes: 7 additions & 0 deletions src/Dev/Deprecation.php
Expand Up @@ -228,16 +228,23 @@ public static function outputNotices(): void
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;
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/Dev/SapphireTest.php
Expand Up @@ -49,6 +49,7 @@
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\View\SSViewer;
use SilverStripe\Dev\Deprecation;

/* -------------------------------------------------
*
Expand Down Expand Up @@ -568,7 +569,9 @@ public function loadFixture($fixtureFile)
{
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());
});
}

/**
Expand Down Expand Up @@ -1008,7 +1011,9 @@ public static function start()
$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
Expand Down
4 changes: 3 additions & 1 deletion src/Security/Member.php
Expand Up @@ -834,7 +834,9 @@ public static function currentUserID()
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 ?? '');
Expand Down
44 changes: 28 additions & 16 deletions src/View/Requirements_Backend.php
Expand Up @@ -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;

Expand Down Expand Up @@ -212,6 +213,7 @@ class Requirements_Backend

/**
* @var Requirements_Minifier
* @deprecated 4.0.1 Will be removed without equivalent functionality
*/
protected $minifier = null;

Expand Down Expand Up @@ -251,9 +253,11 @@ public function getMinifier()
* 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;
}

Expand Down Expand Up @@ -389,9 +393,11 @@ public function getForceJSToBottom()
* 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;
}

Expand All @@ -400,9 +406,11 @@ public function getMinifyCombinedFiles()
*
* @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;
}
Expand Down Expand Up @@ -1381,22 +1389,25 @@ protected function getCombinedFileURL($combinedFile, $fileList, $type)
$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(
<<<MESSAGE
Cannot minify files without a minification service defined.
Set %s::minifyCombinedFiles to false, or inject a %s service on
%s.properties.minifier
MESSAGE
,
__CLASS__,
Requirements_Minifier::class,
__CLASS__
)
);
}
$minify = Deprecation::withNoReplacement(function () {
$minify = $this->getMinifyCombinedFiles();
if ($minify && !$this->minifier) {
throw new Exception(
sprintf(
<<<MESSAGE
Cannot minify files without a minification service defined.
Set %s::minifyCombinedFiles to false, or inject a %s service on
%s.properties.minifier
MESSAGE
,
__CLASS__,
Requirements_Minifier::class,
__CLASS__
)
);
}
return $minify;
});

$combinedURL = $this
->getAssetHandler()
Expand All @@ -1416,6 +1427,7 @@ function () use ($fileList, $minify, $type) {
$fileContent = $this->resolveCSSReferences($fileContent, $file);
}
// Use configured minifier
// @deprecated
if ($minify) {
$fileContent = $this->minifier->minify($fileContent, $type, $file);
}
Expand Down
2 changes: 1 addition & 1 deletion src/View/SSViewer.php
Expand Up @@ -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
*/
Expand Down
10 changes: 9 additions & 1 deletion tests/php/Core/ConvertTest.php
Expand Up @@ -419,7 +419,9 @@ public function testRaw2JsonWithContext()
*/
public function testXML2Array()
{

if (Deprecation::isEnabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$inputXML = <<<XML
<?xml version="1.0"?>
<!DOCTYPE results [
Expand Down Expand Up @@ -449,6 +451,9 @@ public function testXML2Array()
*/
public function testXML2ArrayEntityException()
{
if (Deprecation::isEnabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$inputXML = <<<XML
<?xml version="1.0"?>
<!DOCTYPE results [
Expand All @@ -469,6 +474,9 @@ public function testXML2ArrayEntityException()
*/
public function testXML2ArrayMultipleEntitiesException()
{
if (Deprecation::isEnabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$inputXML = <<<XML
<?xml version="1.0"?>
<!DOCTYPE results [<!ENTITY long "SOME_SUPER_LONG_STRING"><!ENTITY short "SHORT_STRING">]>
Expand Down
8 changes: 4 additions & 4 deletions tests/php/Core/ObjectTest.php
Expand Up @@ -117,7 +117,7 @@ public function testStaticGetterMethod()
$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'
);
}
Expand All @@ -129,7 +129,7 @@ public function testStaticInheritanceGetters()
}
$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'
);
Expand All @@ -144,7 +144,7 @@ public function testStaticSettingOnSingletons()
$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'
);
Expand All @@ -159,7 +159,7 @@ public function testStaticSettingOnInstances()
$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'
);
Expand Down
9 changes: 5 additions & 4 deletions tests/php/Logging/MonologErrorHandlerTest.php
Expand Up @@ -20,17 +20,18 @@ public function testStartThrowsExceptionWithoutLoggerDefined()

public function testSetLoggerResetsStack()
{
if (Deprecation::isEnabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
/** @var LoggerInterface $logger */
$logger = $this->createMock(LoggerInterface::class);

$handler = new MonologErrorHandler();
$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');
Expand Down
5 changes: 4 additions & 1 deletion tests/php/View/RequirementsTest.php
Expand Up @@ -14,6 +14,7 @@
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;

Expand Down Expand Up @@ -302,7 +303,9 @@ protected function setupRequirements($backend)
$backend->clear();
$backend->clearCombinedFiles();
$backend->setCombinedFilesFolder('_combinedfiles');
$backend->setMinifyCombinedFiles(false);
Deprecation::withNoReplacement(function () use ($backend) {
$backend->setMinifyCombinedFiles(false);
});
$backend->setCombinedFilesEnabled(true);
Requirements::flush();
}
Expand Down
3 changes: 3 additions & 0 deletions tests/php/View/SSViewerTest.php
Expand Up @@ -277,6 +277,9 @@ public function testRequirementsCombine()

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);
Expand Down

0 comments on commit b5533e4

Please sign in to comment.