Skip to content
Permalink
Browse files

Fix sprintf warnings when using deprecated properties

Also, deprecated properties now fallback to new getters/setters.
  • Loading branch information...
chinpei215 committed Jan 30, 2017
1 parent ab29b5b commit e2c56381f80663e4075cfc22341a1cf2db5c9580
Showing with 68 additions and 15 deletions.
  1. +19 −15 src/Controller/Controller.php
  2. +49 −0 tests/TestCase/Controller/ControllerTest.php
@@ -332,14 +332,22 @@ public function loadComponent($name, array $config = [])
*/
public function __get($name)
{
if (in_array($name, ['layout', 'view', 'theme', 'autoLayout', 'viewPath', 'layoutPath'], true)) {
$method = $name === 'viewPath' ? 'templatePath' : $name;
$deprecated = [
'layout' => 'getLayout',
'view' => 'getTemplate',
'theme' => 'getTheme',
'autoLayout' => 'isAutoLayoutEnabled',
'viewPath' => 'getTemplatePath',
'layoutPath' => 'getLayoutPath',
];
if (isset($deprecated[$name])) {
$method = $deprecated[$name];
trigger_error(
sprintf('Controller::$%s is deprecated. Use $this->viewBuilder()->set%s()/$this->viewBuilder()->get%s() instead.', $name, ucfirst($method)),
sprintf('Controller::$%s is deprecated. Use $this->viewBuilder()->%s() instead.', $name, $method),
E_USER_DEPRECATED
);
return $this->viewBuilder()->{$name}();
return $this->viewBuilder()->{$method}();
}
list($plugin, $class) = pluginSplit($this->modelClass, true);
@@ -360,21 +368,17 @@ public function __get($name)
public function __set($name, $value)
{
$deprecated = [
'layout' => 'layout',
'view' => 'template',
'theme' => 'theme',
'autoLayout' => 'autoLayout',
'viewPath' => 'templatePath',
'layoutPath' => 'layoutPath',
'layout' => 'setLayout',
'view' => 'setTemplate',
'theme' => 'setTheme',
'autoLayout' => 'enableAutoLayout',
'viewPath' => 'setTemplatePath',
'layoutPath' => 'setLayoutPath',
];
if (isset($deprecated[$name])) {
$method = $deprecated[$name];
trigger_error(
sprintf(
'Controller::$%s is deprecated. Use $this->viewBuilder()->set%s()/$this->viewBuilder()->get%s() instead.',
$name,
ucfirst($method)
),
sprintf('Controller::$%s is deprecated. Use $this->viewBuilder()->%s() instead.', $name, $method),
E_USER_DEPRECATED
);
$this->viewBuilder()->{$method}($value);
@@ -1042,4 +1042,53 @@ public function testBeforeRenderViewVariables()
$this->assertArrayHasKey('testVariable', $controller->View->viewVars);
}
/**
* Tests deprecated view propertiyes work
*
* @param $property Deprecated property name
* @param $getter Getter name
* @param $setter Setter name
* @param mixed $value Value to be set
* @return void
* @dataProvider deprecatedViewPropertyProvider
*/
public function testDeprecatedViewProperty($property, $getter, $setter, $value)
{
$controller = new AnotherTestController();
$message = false;
set_error_handler(function ($errno, $errstr) use (&$message) {
$message = ($errno === E_USER_DEPRECATED ? $errstr : false);
});
try {
$controller->$property = $value;
$this->assertSame(sprintf('Controller::$%s is deprecated. Use $this->viewBuilder()->%s() instead.', $property, $setter), $message);
$this->assertSame($value, $controller->$property);
$this->assertSame(sprintf('Controller::$%s is deprecated. Use $this->viewBuilder()->%s() instead.', $property, $getter), $message);
$this->assertSame($value, $controller->viewBuilder()->{$getter}());
} finally {
restore_error_handler();
}
}
/**
* Data provider for testing deprecated view properties
*
* @return array
*/
public function deprecatedViewPropertyProvider()
{
return [
['layout', 'getLayout', 'setLayout', 'custom'],
['view', 'getTemplate', 'setTemplate', 'view'],
['theme', 'getTheme', 'setTheme', 'Modern'],
['autoLayout', 'isAutoLayoutEnabled', 'enableAutoLayout', false],
['viewPath', 'getTemplatePath', 'setTemplatePath', 'Templates'],
['layoutPath', 'getLayoutPath', 'setLayoutPath', 'Layouts'],
];
}
}

0 comments on commit e2c5638

Please sign in to comment.
You can’t perform that action at this time.