Skip to content

Commit

Permalink
Scoped deprecation messages (fixes #7645)
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Jul 13, 2012
1 parent a5a0853 commit 712f28b
Show file tree
Hide file tree
Showing 24 changed files with 97 additions and 43 deletions.
2 changes: 1 addition & 1 deletion admin/code/LeftAndMainDecorator.php
Expand Up @@ -7,7 +7,7 @@
abstract class LeftAndMainDecorator extends LeftAndMainExtension {

public function __construct() {
Deprecation::notice('3.0', 'Use LeftAndMainExtension instead.');
Deprecation::notice('3.0', 'Use LeftAndMainExtension instead.', Deprecation::SCOPE_CLASS);
parent::__construct();
}

Expand Down
2 changes: 1 addition & 1 deletion control/Director.php
Expand Up @@ -42,7 +42,7 @@ class Director implements TemplateGlobalProvider {
*/
static function addRules($priority, $rules) {
if ($priority != 100) {
Deprecation::notice('3.0', 'Priority argument is now ignored - use the default of 100. You should really be setting routes via _config yaml fragments though.');
Deprecation::notice('3.0', 'Priority argument is now ignored - use the default of 100. You should really be setting routes via _config yaml fragments though.', Deprecation::SCOPE_GLOBAL);
}

Config::inst()->update('Director', 'rules', $rules);
Expand Down
12 changes: 6 additions & 6 deletions core/Object.php
Expand Up @@ -332,7 +332,7 @@ public static function static_lookup($class, $name, $default = null) {
* @return mixed
*/
public static function get_static($class, $name, $uncached = false) {
Deprecation::notice('3.1.0', 'get_static is deprecated, replaced by Config#get');
Deprecation::notice('3.1.0', 'Replaced by Config#get');
return Config::inst()->get($class, $name, Config::FIRST_SET);
}

Expand All @@ -344,7 +344,7 @@ public static function get_static($class, $name, $uncached = false) {
* @param mixed $value
*/
public static function set_static($class, $name, $value) {
Deprecation::notice('3.1.0', 'set_static is deprecated, replaced by Config#update');
Deprecation::notice('3.1.0', 'Replaced by Config#update');
Config::inst()->update($class, $name, $value);
}

Expand All @@ -356,7 +356,7 @@ public static function set_static($class, $name, $value) {
* @return mixed
*/
public static function uninherited_static($class, $name, $uncached = false) {
Deprecation::notice('3.1.0', 'uninherited_static is deprecated, replaced by Config#get');
Deprecation::notice('3.1.0', 'Replaced by Config#get');
return Config::inst()->get($class, $name, Config::UNINHERITED);
}

Expand All @@ -373,7 +373,7 @@ public static function uninherited_static($class, $name, $uncached = false) {
public static function combined_static($class, $name, $ceiling = false) {
if ($ceiling) throw new Exception('Ceiling argument to combined_static is no longer supported');

Deprecation::notice('3.1.0', 'combined_static is deprecated, replaced by Config#get');
Deprecation::notice('3.1.0', 'Replaced by Config#get');
return Config::inst()->get($class, $name);
}

Expand All @@ -385,7 +385,7 @@ public static function combined_static($class, $name, $ceiling = false) {
* @param bool $replace replace existing static vars
*/
public static function addStaticVars($class, $properties, $replace = false) {
Deprecation::notice('3.1.0', 'addStaticVars is deprecated, replaced by Config#update');
Deprecation::notice('3.1.0', 'Replaced by Config#update');
foreach($properties as $prop => $value) self::add_static_var($class, $prop, $value, $replace);
}

Expand All @@ -406,7 +406,7 @@ public static function addStaticVars($class, $properties, $replace = false) {
* @param bool $replace completely replace existing static values
*/
public static function add_static_var($class, $name, $value, $replace = false) {
Deprecation::notice('3.1.0', 'add_static_var is deprecated, replaced by Config#remove and Config#update');
Deprecation::notice('3.1.0', 'Replaced by Config#remove and Config#update');

if ($replace) Config::inst()->remove($class, $name);
Config::inst()->update($class, $name, $value);
Expand Down
27 changes: 22 additions & 5 deletions dev/Deprecation.php
Expand Up @@ -32,6 +32,10 @@
*/
class Deprecation {

const SCOPE_METHOD = 1;
const SCOPE_CLASS = 2;
const SCOPE_GLOBAL = 4;

/**
*
* @var string
Expand Down Expand Up @@ -119,9 +123,10 @@ protected static function get_called_method_from_trace($backtrace, $level = 1) {
* @static
* @param $string - The notice to raise
* @param $atVersion - The version at which this notice should start being raised
* @param Boolean $scope - Notice relates to the method or class context its called in.
* @return void
*/
public static function notice($atVersion, $string = '') {
public static function notice($atVersion, $string = '', $scope = Deprecation::SCOPE_METHOD) {
// Never raise deprecation notices in a live environment
if(Director::isLive()) return;

Expand All @@ -139,9 +144,16 @@ public static function notice($atVersion, $string = '') {

// Check the version against the notice version
if ($checkVersion && version_compare($checkVersion, $atVersion, '>=')) {
// Get the calling method
if (!$backtrace) $backtrace = debug_backtrace(0);
$caller = self::get_called_method_from_trace($backtrace);
// Get the calling scope
if($scope == Deprecation::SCOPE_METHOD) {
if (!$backtrace) $backtrace = debug_backtrace(0);
$caller = self::get_called_method_from_trace($backtrace);
} elseif($scope == Deprecation::SCOPE_CLASS) {
if (!$backtrace) $backtrace = debug_backtrace(0);
$caller = isset($backtrace[1]['class']) ? $backtrace[1]['class'] : '(unknown)';
} else {
$caller = false;
}

// Get the level to raise the notice as
$level = self::$notice_level;
Expand All @@ -152,7 +164,12 @@ public static function notice($atVersion, $string = '') {

$string .= " Called from " . self::get_called_method_from_trace($backtrace, 2) . '.';

user_error($caller.' is deprecated.'.($string ? ' '.$string : ''), $level);
if($caller) {
user_error($caller.' is deprecated.'.($string ? ' '.$string : ''), $level);
} else {
user_error($string, $level);
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion filesystem/Folder.php
Expand Up @@ -41,7 +41,7 @@ public function populateDefaults() {
* @deprecated in favor of the correct name find_or_make
*/
public static function findOrMake($folderPath) {
Deprecation::notice('3.0', "Folder::findOrMake() is deprecated in favor of Folder::find_or_make()");
Deprecation::notice('3.0', "Use Folder::find_or_make() instead.");
return self::find_or_make($folderPath);
}

Expand Down
4 changes: 2 additions & 2 deletions forms/DropdownField.php
Expand Up @@ -118,10 +118,10 @@ function __construct($name, $title = null, $source = array(), $value = '', $form
$this->setSource($source);

if($emptyString === true) {
Deprecation::notice('3.1', 'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument');
Deprecation::notice('3.1', 'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument', Deprecation::SCOPE_GLOBAL);
}
if(is_string($emptyString)) {
Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string $emptyString argument.');
Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string $emptyString argument.', Deprecation::SCOPE_GLOBAL);
}

if($emptyString) $this->setHasEmptyDefault(true);
Expand Down
13 changes: 8 additions & 5 deletions forms/FieldList.php
Expand Up @@ -605,11 +605,14 @@ protected function rewriteTabPath($name) {
foreach($this->getTabPathRewrites() as $regex => $replace) {
if(preg_match($regex, $name)) {
$newName = preg_replace($regex, $replace, $name);
Deprecation::notice('3.0.0', sprintf(
'Using outdated tab path "%s", please use the new location "%s" instead',
$name,
$newName
));
Deprecation::notice('3.0.0',
sprintf(
'Using outdated tab path "%s", please use the new location "%s" instead',
$name,
$newName
),
Deprecation::SCOPE_GLOBAL
);
return $newName;
}
}
Expand Down
8 changes: 7 additions & 1 deletion forms/FileField.php
Expand Up @@ -102,7 +102,13 @@ class FileField extends FormField {
* @param int $value The value of the field.
*/
function __construct($name, $title = null, $value = null) {
if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRightTitle() and setFolderName() instead of constructor arguments');
if(count(func_get_args()) > 3) {
Deprecation::notice(
'3.0',
'Use setRightTitle() and setFolderName() instead of constructor arguments',
Deprecation::SCOPE_GLOBAL
);
}

$this->upload = new Upload();

Expand Down
2 changes: 1 addition & 1 deletion forms/HasManyComplexTableField.php
Expand Up @@ -51,7 +51,7 @@ class HasManyComplexTableField extends ComplexTableField {
function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);

Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor');
Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor', Deprecation::SCOPE_CLASS);

$this->Markable = true;

Expand Down
2 changes: 1 addition & 1 deletion forms/HtmlEditorField.php
Expand Up @@ -46,7 +46,7 @@ public static function include_js() {
* @see TextareaField::__construct()
*/
public function __construct($name, $title = null, $value = '') {
if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRows() and setCols() instead of constructor arguments');
if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRows() and setCols() instead of constructor arguments', Deprecation::SCOPE_GLOBAL);

parent::__construct($name, $title, $value);

Expand Down
2 changes: 1 addition & 1 deletion forms/ImageField.php
Expand Up @@ -45,7 +45,7 @@ public function FileTypeName() {
* @return Form
*/
public function EditFileForm() {
Deprecation::notice('3.0', 'Use UploadField');
Deprecation::notice('3.0', 'Use UploadField', Deprecation::SCOPE_CLASS);

$filter = create_function('$item', 'return (in_array("Folder", ClassInfo::ancestry($item->ClassName)) || in_array("Image", ClassInfo::ancestry($item->ClassName)));');

Expand Down
2 changes: 1 addition & 1 deletion forms/ImageFormAction.php
Expand Up @@ -18,7 +18,7 @@ class ImageFormAction extends FormAction {
* @param form The parent form, auto-set when the field is placed inside a form
*/
function __construct($action, $title = "", $image = "", $hoverImage = null, $className = null, $form = null) {
Deprecation::notice('3.0', "Use FormAction with setAttribute('src', 'myimage.png') and custom JavaScript to achieve hover effect");
Deprecation::notice('3.0', "Use FormAction with setAttribute('src', 'myimage.png') and custom JavaScript to achieve hover effect", Deprecation::SCOPE_CLASS);

$this->image = $image;
$this->hoverImage = $hoverImage;
Expand Down
2 changes: 1 addition & 1 deletion forms/ManyManyComplexTableField.php
Expand Up @@ -43,7 +43,7 @@ class ManyManyComplexTableField extends HasManyComplexTableField {

function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {

Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor');
Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor', Deprecation::SCOPE_CLASS);

parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);

Expand Down
2 changes: 1 addition & 1 deletion forms/PasswordField.php
Expand Up @@ -11,7 +11,7 @@ class PasswordField extends TextField {
* maxlength
*/
function __construct($name, $title = null, $value = "") {
if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setMaxLength() instead of constructor arguments');
if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setMaxLength() instead of constructor arguments', Deprecation::SCOPE_GLOBAL);

parent::__construct($name, $title, $value);
}
Expand Down
4 changes: 2 additions & 2 deletions forms/SimpleImageField.php
Expand Up @@ -69,9 +69,9 @@
class SimpleImageField extends FileField {

function __construct($name, $title = null, $value = null) {
Deprecation::notice('3.0', "Use UploadField with \$myField->allowedExtensions = array('jpg', 'gif', 'png')");
Deprecation::notice('3.0', "SimpleImageField is deprecated. Use UploadField with \$myField->allowedExtensions = array('jpg', 'gif', 'png')", Deprecation::SCOPE_CLASS);

if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRightTitle() and setFolderName() instead of constructor arguments');
if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRightTitle() and setFolderName() instead of constructor arguments', Deprecation::SCOPE_GLOBAL);

parent::__construct($name, $title, $value);

Expand Down
3 changes: 2 additions & 1 deletion i18n/i18n.php
Expand Up @@ -1465,7 +1465,8 @@ static function _t($entity, $string = "", $context = "", $injection = "") {
if(is_numeric($context) && in_array($context, array(PR_LOW, PR_MEDIUM, PR_HIGH))) {
Deprecation::notice(
'3.0',
'The $priority argument to _t() is deprecated, please use module inclusion priorities instead'
'The $priority argument to _t() is deprecated, please use module inclusion priorities instead',
Deprecation::SCOPE_GLOBAL
);
}

Expand Down
2 changes: 1 addition & 1 deletion model/ComponentSet.php
Expand Up @@ -4,6 +4,6 @@
*/
class ComponentSet extends DataObjectSet {
function setComponentInfo($type, $ownerObj, $ownerClass, $tableName, $childClass, $joinField = null) {
Deprecation::notice('3.0', 'Use ManyManyList or HasManyList instead.');
Deprecation::notice('3.0', 'ComponentSet is deprecated. Use ManyManyList or HasManyList instead.', Deprecation::SCOPE_CLASS);
}
}
2 changes: 1 addition & 1 deletion model/DataExtension.php
Expand Up @@ -41,7 +41,7 @@ static function add_to_class($class, $extensionClass, $args = null) {
$statics = Injector::inst()->get($extensionClass, true, $args)->$extraStaticsMethod($class, $extensionClass);

if ($statics) {
Deprecation::notice('3.1.0', "$extraStaticsMethod deprecated. Just define statics on your extension, or use add_to_class");
Deprecation::notice('3.1.0', "$extraStaticsMethod deprecated. Just define statics on your extension, or use add_to_class", Deprecation::SCOPE_GLOBAL);

// TODO: This currently makes extraStatics the MOST IMPORTANT config layer, not the least
foreach (self::$extendable_statics as $key => $merge) {
Expand Down
4 changes: 2 additions & 2 deletions model/DataList.php
Expand Up @@ -141,7 +141,7 @@ public function limit($limit, $offset = 0) {
return $this;
}
if($limit && !is_numeric($limit)) {
Deprecation::notice('3.0', 'Please pass limits as 2 arguments, rather than an array or SQL fragment.');
Deprecation::notice('3.0', 'Please pass limits as 2 arguments, rather than an array or SQL fragment.', Deprecation::SCOPE_GLOBAL);
}
$this->dataQuery->limit($limit, $offset);
return $this;
Expand Down Expand Up @@ -599,7 +599,7 @@ public function exists() {
* @return DataList
*/
public function getRange($offset, $length) {
Deprecation::notice("3.0", 'getRange($offset, $length) is deprecated. Use limit($length, $offset) instead. Note the new argument order.');
Deprecation::notice("3.0", 'Use limit($length, $offset) instead. Note the new argument order.');
return $this->limit($length, $offset);
}

Expand Down
2 changes: 1 addition & 1 deletion model/DataObject.php
Expand Up @@ -2596,7 +2596,7 @@ public static function get($callerClass = null, $filter = "", $sort = "", $join
// Todo: Determine if we can deprecate for 3.0.0 and use DI or something instead
// Todo: Make the $containerClass method redundant
if($containerClass != 'DataList') {
Deprecation::notice('3.0', '$containerClass argument is deprecated.');
Deprecation::notice('3.0', 'DataObject::get() - $containerClass argument is deprecated.', Deprecation::SCOPE_GLOBAL);
}

$result = DataList::create($callerClass)->where($filter)->sort($sort);
Expand Down
2 changes: 1 addition & 1 deletion model/DataObjectDecorator.php
Expand Up @@ -7,7 +7,7 @@
abstract class DataObjectDecorator extends DataExtension {

public function __construct() {
Deprecation::notice('3.0', 'Use DataExtension instead.');
Deprecation::notice('3.0', 'DataObjectDecorator is deprecated. Use DataExtension instead.', Deprecation::SCOPE_CLASS);
parent::__construct();
}

Expand Down
2 changes: 1 addition & 1 deletion model/DataObjectSet.php
Expand Up @@ -10,7 +10,7 @@ class DataObjectSet extends ArrayList {
* @deprecated 3.0
*/
public function __construct($items = array()) {
Deprecation::notice('3.0', 'Use DataList or ArrayList instead');
Deprecation::notice('3.0', 'DataObjectSet is deprecated. Use DataList or ArrayList instead', Deprecation::SCOPE_CLASS);

if ($items) {
if (!is_array($items) || func_num_args() > 1) {
Expand Down
2 changes: 1 addition & 1 deletion model/SQLMap.php
Expand Up @@ -19,7 +19,7 @@ class SQLMap extends Object implements IteratorAggregate {
* @param SQLQuery $query The query to generate this map. THis isn't executed until it's needed.
*/
public function __construct(SQLQuery $query, $keyField = "ID", $titleField = "Title") {
Deprecation::notice('3.0', 'Use SS_Map or DataList::map() instead.');
Deprecation::notice('3.0', 'Use SS_Map or DataList::map() instead.', Deprecation::SCOPE_CLASS);

if(!$query) {
user_error('SQLMap constructed with null query.', E_USER_ERROR);
Expand Down
35 changes: 31 additions & 4 deletions tests/dev/DeprecationTest.php
Expand Up @@ -66,13 +66,40 @@ function testMatchingModuleNotifcationVersionAffectsNotice() {
$this->callThatOriginatesFromFramework();
}

function testMethodNameCalculation() {
$this->assertEquals(DeprecationTest_Deprecation::get_method(), 'DeprecationTest->testMethodNameCalculation');
}

/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage DeprecationTest->testScopeMethod is deprecated. Method scope
*/
function testScopeMethod() {
Deprecation::notification_version('2.0.0');
Deprecation::notice('2.0.0', 'Method scope', Deprecation::SCOPE_METHOD);
}

/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage DeprecationTest is deprecated. Class scope
*/
function testScopeClass() {
Deprecation::notification_version('2.0.0');
Deprecation::notice('2.0.0', 'Class scope', Deprecation::SCOPE_CLASS);
}

/**
* @expectedException PHPUnit_Framework_Error
* @expectedExceptionMessage Global scope
*/
function testScopeGlobal() {
Deprecation::notification_version('2.0.0');
Deprecation::notice('2.0.0', 'Global scope', Deprecation::SCOPE_GLOBAL);
}

protected function callThatOriginatesFromFramework() {
$this->assertEquals(DeprecationTest_Deprecation::get_module(), FRAMEWORK_DIR);
Deprecation::notice('2.0', 'Deprecation test passed');
}

function testMethodNameCalculation() {
$this->assertEquals(DeprecationTest_Deprecation::get_method(), 'DeprecationTest->testMethodNameCalculation');
}

}

0 comments on commit 712f28b

Please sign in to comment.