Scoped deprecation messages (fixes #7645)

This commit is contained in:
Ingo Schommer 2012-07-13 11:37:35 +02:00
parent a5a08530fc
commit 712f28bc78
24 changed files with 97 additions and 43 deletions

View File

@ -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();
}

View File

@ -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);

View File

@ -332,7 +332,7 @@ abstract class Object {
* @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);
}
@ -344,7 +344,7 @@ abstract class Object {
* @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);
}
@ -356,7 +356,7 @@ abstract class Object {
* @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);
}
@ -373,7 +373,7 @@ abstract class Object {
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);
}
@ -385,7 +385,7 @@ abstract class Object {
* @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);
}
@ -406,7 +406,7 @@ abstract class Object {
* @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);

View File

@ -32,6 +32,10 @@
*/
class Deprecation {
const SCOPE_METHOD = 1;
const SCOPE_CLASS = 2;
const SCOPE_GLOBAL = 4;
/**
*
* @var string
@ -119,9 +123,10 @@ class Deprecation {
* @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;
@ -139,9 +144,16 @@ class Deprecation {
// 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;
@ -152,7 +164,12 @@ class Deprecation {
$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);
}
}
}

View File

@ -41,7 +41,7 @@ class Folder extends File {
* @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);
}

View File

@ -118,10 +118,10 @@ class DropdownField extends FormField {
$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);

View File

@ -605,11 +605,14 @@ class FieldList extends ArrayList {
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;
}
}

View File

@ -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();

View File

@ -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;

View File

@ -46,7 +46,7 @@ class HtmlEditorField extends TextareaField {
* @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);

View File

@ -45,7 +45,7 @@ class ImageField extends FileIFrameField {
* @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)));');

View File

@ -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;

View File

@ -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);

View File

@ -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);
}

View File

@ -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);

View File

@ -1465,7 +1465,8 @@ class i18n extends Object implements TemplateGlobalProvider {
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
);
}

View File

@ -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);
}
}

View File

@ -41,7 +41,7 @@ abstract class DataExtension extends Extension {
$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) {

View File

@ -141,7 +141,7 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
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;
@ -599,7 +599,7 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
* @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);
}

View File

@ -2596,7 +2596,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
// 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);

View File

@ -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();
}

View File

@ -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) {

View File

@ -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);

View File

@ -66,13 +66,40 @@ class DeprecationTest extends SapphireTest {
$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');
}
}