Skip to content
This repository
Browse code

Scoped deprecation messages (fixes #7645)

  • Loading branch information...
commit 712f28bc78fa49018f36c5abc3c305a53c58b724 1 parent a5a0853
Ingo Schommer authored July 13, 2012
2  admin/code/LeftAndMainDecorator.php
@@ -7,7 +7,7 @@
7 7
 abstract class LeftAndMainDecorator extends LeftAndMainExtension {
8 8
 	
9 9
 	public function __construct() {
10  
-		Deprecation::notice('3.0', 'Use LeftAndMainExtension instead.');
  10
+		Deprecation::notice('3.0', 'Use LeftAndMainExtension instead.', Deprecation::SCOPE_CLASS);
11 11
 		parent::__construct();
12 12
 	}
13 13
 	
2  control/Director.php
@@ -42,7 +42,7 @@ class Director implements TemplateGlobalProvider {
42 42
 	 */
43 43
 	static function addRules($priority, $rules) {
44 44
 		if ($priority != 100) {
45  
-			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.');
  45
+			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);
46 46
 		}
47 47
 
48 48
 		Config::inst()->update('Director', 'rules', $rules);
12  core/Object.php
@@ -332,7 +332,7 @@ public static function static_lookup($class, $name, $default = null) {
332 332
 	 * @return mixed
333 333
 	 */
334 334
 	public static function get_static($class, $name, $uncached = false) {
335  
-		Deprecation::notice('3.1.0', 'get_static is deprecated, replaced by Config#get');
  335
+		Deprecation::notice('3.1.0', 'Replaced by Config#get');
336 336
 		return Config::inst()->get($class, $name, Config::FIRST_SET);
337 337
 	}
338 338
 
@@ -344,7 +344,7 @@ public static function get_static($class, $name, $uncached = false) {
344 344
 	 * @param mixed $value
345 345
 	 */
346 346
 	public static function set_static($class, $name, $value) {
347  
-		Deprecation::notice('3.1.0', 'set_static is deprecated, replaced by Config#update');
  347
+		Deprecation::notice('3.1.0', 'Replaced by Config#update');
348 348
 		Config::inst()->update($class, $name, $value);
349 349
 	}
350 350
 
@@ -356,7 +356,7 @@ public static function set_static($class, $name, $value) {
356 356
 	 * @return mixed
357 357
 	 */
358 358
 	public static function uninherited_static($class, $name, $uncached = false) {
359  
-		Deprecation::notice('3.1.0', 'uninherited_static is deprecated, replaced by Config#get');
  359
+		Deprecation::notice('3.1.0', 'Replaced by Config#get');
360 360
 		return Config::inst()->get($class, $name, Config::UNINHERITED);
361 361
 	}
362 362
 	
@@ -373,7 +373,7 @@ public static function uninherited_static($class, $name, $uncached = false) {
373 373
 	public static function combined_static($class, $name, $ceiling = false) {
374 374
 		if ($ceiling) throw new Exception('Ceiling argument to combined_static is no longer supported');
375 375
 
376  
-		Deprecation::notice('3.1.0', 'combined_static is deprecated, replaced by Config#get');
  376
+		Deprecation::notice('3.1.0', 'Replaced by Config#get');
377 377
 		return Config::inst()->get($class, $name);
378 378
 	}
379 379
 	
@@ -385,7 +385,7 @@ public static function combined_static($class, $name, $ceiling = false) {
385 385
 	 * @param bool $replace replace existing static vars
386 386
 	 */
387 387
 	public static function addStaticVars($class, $properties, $replace = false) {
388  
-		Deprecation::notice('3.1.0', 'addStaticVars is deprecated, replaced by Config#update');
  388
+		Deprecation::notice('3.1.0', 'Replaced by Config#update');
389 389
 		foreach($properties as $prop => $value) self::add_static_var($class, $prop, $value, $replace);
390 390
 	}
391 391
 	
@@ -406,7 +406,7 @@ public static function addStaticVars($class, $properties, $replace = false) {
406 406
 	 * @param bool $replace completely replace existing static values
407 407
 	 */
408 408
 	public static function add_static_var($class, $name, $value, $replace = false) {
409  
-		Deprecation::notice('3.1.0', 'add_static_var is deprecated, replaced by Config#remove and Config#update');
  409
+		Deprecation::notice('3.1.0', 'Replaced by Config#remove and Config#update');
410 410
 
411 411
 		if ($replace) Config::inst()->remove($class, $name);
412 412
 		Config::inst()->update($class, $name, $value);
27  dev/Deprecation.php
@@ -32,6 +32,10 @@
32 32
  */
33 33
 class Deprecation {
34 34
 
  35
+	const SCOPE_METHOD = 1;
  36
+	const SCOPE_CLASS = 2;
  37
+	const SCOPE_GLOBAL = 4;
  38
+
35 39
 	/**
36 40
 	 *
37 41
 	 * @var string
@@ -119,9 +123,10 @@ protected static function get_called_method_from_trace($backtrace, $level = 1) {
119 123
 	 * @static
120 124
 	 * @param $string - The notice to raise
121 125
 	 * @param $atVersion - The version at which this notice should start being raised
  126
+	 * @param Boolean $scope - Notice relates to the method or class context its called in.
122 127
 	 * @return void
123 128
 	 */
124  
-	public static function notice($atVersion, $string = '') {
  129
+	public static function notice($atVersion, $string = '', $scope = Deprecation::SCOPE_METHOD) {
125 130
 		// Never raise deprecation notices in a live environment
126 131
 		if(Director::isLive()) return;
127 132
 
@@ -139,9 +144,16 @@ public static function notice($atVersion, $string = '') {
139 144
 
140 145
 		// Check the version against the notice version
141 146
 		if ($checkVersion && version_compare($checkVersion, $atVersion, '>=')) {
142  
-			// Get the calling method
143  
-			if (!$backtrace) $backtrace = debug_backtrace(0);
144  
-			$caller = self::get_called_method_from_trace($backtrace);
  147
+			// Get the calling scope
  148
+			if($scope == Deprecation::SCOPE_METHOD) {
  149
+				if (!$backtrace) $backtrace = debug_backtrace(0);
  150
+				$caller = self::get_called_method_from_trace($backtrace);
  151
+			} elseif($scope == Deprecation::SCOPE_CLASS) {
  152
+				if (!$backtrace) $backtrace = debug_backtrace(0);
  153
+				$caller = isset($backtrace[1]['class']) ? $backtrace[1]['class'] : '(unknown)';
  154
+			} else {
  155
+				$caller = false;
  156
+			}
145 157
 
146 158
 			// Get the level to raise the notice as
147 159
 			$level = self::$notice_level;
@@ -152,7 +164,12 @@ public static function notice($atVersion, $string = '') {
152 164
 
153 165
 			$string .= " Called from " . self::get_called_method_from_trace($backtrace, 2) . '.';
154 166
 
155  
-			user_error($caller.' is deprecated.'.($string ? ' '.$string : ''), $level);
  167
+			if($caller) {
  168
+				user_error($caller.' is deprecated.'.($string ? ' '.$string : ''), $level);	
  169
+			} else {
  170
+				user_error($string, $level);	
  171
+			}
  172
+			
156 173
 		}
157 174
 	}
158 175
 
2  filesystem/Folder.php
@@ -41,7 +41,7 @@ public function populateDefaults() {
41 41
 	 * @deprecated in favor of the correct name find_or_make
42 42
 	 */
43 43
 	public static function findOrMake($folderPath) {
44  
-		Deprecation::notice('3.0', "Folder::findOrMake() is deprecated in favor of Folder::find_or_make()");
  44
+		Deprecation::notice('3.0', "Use Folder::find_or_make() instead.");
45 45
 		return self::find_or_make($folderPath);
46 46
 	}
47 47
 	
4  forms/DropdownField.php
@@ -118,10 +118,10 @@ function __construct($name, $title = null, $source = array(), $value = '', $form
118 118
 		$this->setSource($source);
119 119
 
120 120
 		if($emptyString === true) {
121  
-			Deprecation::notice('3.1', 'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument');
  121
+			Deprecation::notice('3.1', 'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument', Deprecation::SCOPE_GLOBAL);
122 122
 		}
123 123
 		if(is_string($emptyString)) {
124  
-			Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string $emptyString argument.');
  124
+			Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string $emptyString argument.', Deprecation::SCOPE_GLOBAL);
125 125
 		}
126 126
 
127 127
 		if($emptyString) $this->setHasEmptyDefault(true);
13  forms/FieldList.php
@@ -605,11 +605,14 @@ protected function rewriteTabPath($name) {
605 605
 		foreach($this->getTabPathRewrites() as $regex => $replace) {
606 606
 			if(preg_match($regex, $name)) {
607 607
 				$newName = preg_replace($regex, $replace, $name);
608  
-				Deprecation::notice('3.0.0', sprintf(
609  
-					'Using outdated tab path "%s", please use the new location "%s" instead',
610  
-					$name,
611  
-					$newName
612  
-				));
  608
+				Deprecation::notice('3.0.0', 
  609
+					sprintf(
  610
+						'Using outdated tab path "%s", please use the new location "%s" instead',
  611
+						$name,
  612
+						$newName
  613
+					),
  614
+					Deprecation::SCOPE_GLOBAL
  615
+				);
613 616
 				return $newName;
614 617
 			}
615 618
 		}
8  forms/FileField.php
@@ -102,7 +102,13 @@ class FileField extends FormField {
102 102
 	 * @param int $value The value of the field.
103 103
 	 */
104 104
 	function __construct($name, $title = null, $value = null) {
105  
-		if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRightTitle() and setFolderName() instead of constructor arguments');
  105
+		if(count(func_get_args()) > 3) {
  106
+			Deprecation::notice(
  107
+				'3.0', 
  108
+				'Use setRightTitle() and setFolderName() instead of constructor arguments', 
  109
+				Deprecation::SCOPE_GLOBAL
  110
+			);
  111
+		}
106 112
 
107 113
 		$this->upload = new Upload();
108 114
 	
2  forms/HasManyComplexTableField.php
@@ -51,7 +51,7 @@ class HasManyComplexTableField extends ComplexTableField {
51 51
 	function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
52 52
 		parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
53 53
 
54  
-		Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor');
  54
+		Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor', Deprecation::SCOPE_CLASS);
55 55
 		
56 56
 		$this->Markable = true;
57 57
 
2  forms/HtmlEditorField.php
@@ -46,7 +46,7 @@ public static function include_js() {
46 46
 	 * @see TextareaField::__construct()
47 47
 	 */
48 48
 	public function __construct($name, $title = null, $value = '') {
49  
-		if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRows() and setCols() instead of constructor arguments');
  49
+		if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRows() and setCols() instead of constructor arguments', Deprecation::SCOPE_GLOBAL);
50 50
 
51 51
 		parent::__construct($name, $title, $value);
52 52
 		
2  forms/ImageField.php
@@ -45,7 +45,7 @@ public function FileTypeName() {
45 45
 	 * @return Form
46 46
 	 */
47 47
 	public function EditFileForm() {
48  
-		Deprecation::notice('3.0', 'Use UploadField');
  48
+		Deprecation::notice('3.0', 'Use UploadField', Deprecation::SCOPE_CLASS);
49 49
 
50 50
 		$filter = create_function('$item', 'return (in_array("Folder", ClassInfo::ancestry($item->ClassName)) || in_array("Image", ClassInfo::ancestry($item->ClassName)));');
51 51
 		
2  forms/ImageFormAction.php
@@ -18,7 +18,7 @@ class ImageFormAction extends FormAction {
18 18
 	 * @param form The parent form, auto-set when the field is placed inside a form 
19 19
 	 */
20 20
 	function __construct($action, $title = "", $image = "", $hoverImage = null, $className = null, $form = null) {
21  
-		Deprecation::notice('3.0', "Use FormAction with setAttribute('src', 'myimage.png') and custom JavaScript to achieve hover effect");
  21
+		Deprecation::notice('3.0', "Use FormAction with setAttribute('src', 'myimage.png') and custom JavaScript to achieve hover effect", Deprecation::SCOPE_CLASS);
22 22
 
23 23
 		$this->image = $image;
24 24
 		$this->hoverImage = $hoverImage;
2  forms/ManyManyComplexTableField.php
@@ -43,7 +43,7 @@ class ManyManyComplexTableField extends HasManyComplexTableField {
43 43
 		
44 44
 	function __construct($controller, $name, $sourceClass, $fieldList = null, $detailFormFields = null, $sourceFilter = "", $sourceSort = "", $sourceJoin = "") {
45 45
 
46  
-		Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor');
  46
+		Deprecation::notice('3.0', 'Use GridField with GridFieldConfig_RelationEditor', Deprecation::SCOPE_CLASS);
47 47
 
48 48
 		parent::__construct($controller, $name, $sourceClass, $fieldList, $detailFormFields, $sourceFilter, $sourceSort, $sourceJoin);
49 49
 		
2  forms/PasswordField.php
@@ -11,7 +11,7 @@ class PasswordField extends TextField {
11 11
 	 * maxlength
12 12
 	 */
13 13
 	function __construct($name, $title = null, $value = "") {
14  
-		if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setMaxLength() instead of constructor arguments');
  14
+		if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setMaxLength() instead of constructor arguments', Deprecation::SCOPE_GLOBAL);
15 15
 
16 16
 		parent::__construct($name, $title, $value);
17 17
 	}
4  forms/SimpleImageField.php
@@ -69,9 +69,9 @@
69 69
 class SimpleImageField extends FileField {
70 70
 
71 71
 	function __construct($name, $title = null, $value = null) {
72  
-		Deprecation::notice('3.0', "Use UploadField with \$myField->allowedExtensions = array('jpg', 'gif', 'png')");
  72
+		Deprecation::notice('3.0', "SimpleImageField is deprecated. Use UploadField with \$myField->allowedExtensions = array('jpg', 'gif', 'png')", Deprecation::SCOPE_CLASS);
73 73
 
74  
-		if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRightTitle() and setFolderName() instead of constructor arguments');
  74
+		if(count(func_get_args()) > 3) Deprecation::notice('3.0', 'Use setRightTitle() and setFolderName() instead of constructor arguments', Deprecation::SCOPE_GLOBAL);
75 75
 
76 76
 		parent::__construct($name, $title, $value);
77 77
 
3  i18n/i18n.php
@@ -1465,7 +1465,8 @@ static function _t($entity, $string = "", $context = "", $injection = "") {
1465 1465
 		if(is_numeric($context) && in_array($context, array(PR_LOW, PR_MEDIUM, PR_HIGH))) {
1466 1466
 			Deprecation::notice(
1467 1467
 				'3.0',
1468  
-				'The $priority argument to _t() is deprecated, please use module inclusion priorities instead'
  1468
+				'The $priority argument to _t() is deprecated, please use module inclusion priorities instead',
  1469
+				Deprecation::SCOPE_GLOBAL
1469 1470
 			);
1470 1471
 		}
1471 1472
 
2  model/ComponentSet.php
@@ -4,6 +4,6 @@
4 4
  */
5 5
 class ComponentSet extends DataObjectSet {
6 6
 	function setComponentInfo($type, $ownerObj, $ownerClass, $tableName, $childClass, $joinField = null) {
7  
-		Deprecation::notice('3.0', 'Use ManyManyList or HasManyList instead.');
  7
+		Deprecation::notice('3.0', 'ComponentSet is deprecated. Use ManyManyList or HasManyList instead.', Deprecation::SCOPE_CLASS);
8 8
 	}
9 9
 }
2  model/DataExtension.php
@@ -41,7 +41,7 @@ static function add_to_class($class, $extensionClass, $args = null) {
41 41
 		$statics = Injector::inst()->get($extensionClass, true, $args)->$extraStaticsMethod($class, $extensionClass);
42 42
 
43 43
 		if ($statics) {
44  
-			Deprecation::notice('3.1.0', "$extraStaticsMethod deprecated. Just define statics on your extension, or use add_to_class");
  44
+			Deprecation::notice('3.1.0', "$extraStaticsMethod deprecated. Just define statics on your extension, or use add_to_class", Deprecation::SCOPE_GLOBAL);
45 45
 
46 46
 			// TODO: This currently makes extraStatics the MOST IMPORTANT config layer, not the least
47 47
 			foreach (self::$extendable_statics as $key => $merge) {
4  model/DataList.php
@@ -141,7 +141,7 @@ public function limit($limit, $offset = 0) {
141 141
 			return $this;
142 142
 		}
143 143
 		if($limit && !is_numeric($limit)) {
144  
-			Deprecation::notice('3.0', 'Please pass limits as 2 arguments, rather than an array or SQL fragment.');
  144
+			Deprecation::notice('3.0', 'Please pass limits as 2 arguments, rather than an array or SQL fragment.', Deprecation::SCOPE_GLOBAL);
145 145
 		}
146 146
 		$this->dataQuery->limit($limit, $offset);
147 147
 		return $this;
@@ -599,7 +599,7 @@ public function exists() {
599 599
 	 * @return DataList
600 600
 	 */
601 601
 	public function getRange($offset, $length) {
602  
-		Deprecation::notice("3.0", 'getRange($offset, $length) is deprecated.  Use limit($length, $offset) instead.  Note the new argument order.');
  602
+		Deprecation::notice("3.0", 'Use limit($length, $offset) instead.  Note the new argument order.');
603 603
 		return $this->limit($length, $offset);
604 604
 	}
605 605
 	
2  model/DataObject.php
@@ -2596,7 +2596,7 @@ public static function get($callerClass = null, $filter = "", $sort = "", $join
2596 2596
 		// Todo: Determine if we can deprecate for 3.0.0 and use DI or something instead
2597 2597
 		// Todo: Make the $containerClass method redundant
2598 2598
 		if($containerClass != 'DataList') {
2599  
-			Deprecation::notice('3.0', '$containerClass argument is deprecated.');
  2599
+			Deprecation::notice('3.0', 'DataObject::get() - $containerClass argument is deprecated.', Deprecation::SCOPE_GLOBAL);
2600 2600
 		}
2601 2601
 
2602 2602
 		$result = DataList::create($callerClass)->where($filter)->sort($sort);
2  model/DataObjectDecorator.php
@@ -7,7 +7,7 @@
7 7
 abstract class DataObjectDecorator extends DataExtension {
8 8
 
9 9
 	public function __construct() {
10  
-		Deprecation::notice('3.0', 'Use DataExtension instead.');
  10
+		Deprecation::notice('3.0', 'DataObjectDecorator is deprecated. Use DataExtension instead.', Deprecation::SCOPE_CLASS);
11 11
 		parent::__construct();
12 12
 	}
13 13
 
2  model/DataObjectSet.php
@@ -10,7 +10,7 @@ class DataObjectSet extends ArrayList {
10 10
 	 * @deprecated 3.0
11 11
 	 */
12 12
 	public function __construct($items = array()) {
13  
-		Deprecation::notice('3.0', 'Use DataList or ArrayList instead');
  13
+		Deprecation::notice('3.0', 'DataObjectSet is deprecated. Use DataList or ArrayList instead', Deprecation::SCOPE_CLASS);
14 14
 
15 15
 		if ($items) {
16 16
 			if (!is_array($items) || func_num_args() > 1) {
2  model/SQLMap.php
@@ -19,7 +19,7 @@ class SQLMap extends Object implements IteratorAggregate {
19 19
 	 * @param SQLQuery $query The query to generate this map. THis isn't executed until it's needed.
20 20
 	 */
21 21
 	public function __construct(SQLQuery $query, $keyField = "ID", $titleField = "Title") {
22  
-		Deprecation::notice('3.0', 'Use SS_Map or DataList::map() instead.');
  22
+		Deprecation::notice('3.0', 'Use SS_Map or DataList::map() instead.', Deprecation::SCOPE_CLASS);
23 23
 		
24 24
 		if(!$query) {
25 25
 			user_error('SQLMap constructed with null query.', E_USER_ERROR);
35  tests/dev/DeprecationTest.php
@@ -66,13 +66,40 @@ function testMatchingModuleNotifcationVersionAffectsNotice() {
66 66
 		$this->callThatOriginatesFromFramework();
67 67
 	}
68 68
 
  69
+	function testMethodNameCalculation() {
  70
+		$this->assertEquals(DeprecationTest_Deprecation::get_method(), 'DeprecationTest->testMethodNameCalculation');
  71
+	}
  72
+
  73
+	/**
  74
+	 * @expectedException PHPUnit_Framework_Error
  75
+	 * @expectedExceptionMessage DeprecationTest->testScopeMethod is deprecated. Method scope
  76
+	 */
  77
+	function testScopeMethod() {
  78
+		Deprecation::notification_version('2.0.0');
  79
+		Deprecation::notice('2.0.0', 'Method scope', Deprecation::SCOPE_METHOD);
  80
+	}
  81
+
  82
+	/**
  83
+	 * @expectedException PHPUnit_Framework_Error
  84
+	 * @expectedExceptionMessage DeprecationTest is deprecated. Class scope
  85
+	 */
  86
+	function testScopeClass() {
  87
+		Deprecation::notification_version('2.0.0');
  88
+		Deprecation::notice('2.0.0', 'Class scope', Deprecation::SCOPE_CLASS);
  89
+	}
  90
+
  91
+	/**
  92
+	 * @expectedException PHPUnit_Framework_Error
  93
+	 * @expectedExceptionMessage Global scope
  94
+	 */
  95
+	function testScopeGlobal() {
  96
+		Deprecation::notification_version('2.0.0');
  97
+		Deprecation::notice('2.0.0', 'Global scope', Deprecation::SCOPE_GLOBAL);
  98
+	}
  99
+
69 100
 	protected function callThatOriginatesFromFramework() {
70 101
 		$this->assertEquals(DeprecationTest_Deprecation::get_module(), FRAMEWORK_DIR);
71 102
 		Deprecation::notice('2.0', 'Deprecation test passed');
72 103
 	}
73 104
 
74  
-	function testMethodNameCalculation() {
75  
-		$this->assertEquals(DeprecationTest_Deprecation::get_method(), 'DeprecationTest->testMethodNameCalculation');
76  
-	}
77  
-
78 105
 }

0 notes on commit 712f28b

Please sign in to comment.
Something went wrong with that request. Please try again.