Merge pull request #304 from halkyon/e_strict_fixes

Enable E_STRICT by default for development, fix all E_STRICT errors affected by tests.
This commit is contained in:
Sam Minnée 2012-04-11 17:54:18 -07:00
commit d766100335
27 changed files with 119 additions and 142 deletions

View File

@ -38,8 +38,9 @@
///////////////////////////////////////////////////////////////////////////////
// ENVIRONMENT CONFIG
if(defined('E_DEPRECATED')) error_reporting(E_ALL & ~(E_STRICT));
else error_reporting(E_ALL);
// ALL errors are reported, including E_STRICT by default *unless* the site is in
// live mode, where reporting is limited to fatal errors and warnings (see later in this file)
error_reporting(E_ALL | E_STRICT);
/**
* Include _ss_environment.php files
@ -253,13 +254,11 @@ SS_TemplateLoader::instance()->pushManifest(new SS_TemplateManifest(
BASE_PATH, false, isset($_GET['flush'])
));
// If this is a dev site, enable php error reporting
// This is necessary to force developers to acknowledge and fix
// notice level errors (you can override this directive in your _config.php)
if (Director::isLive()) {
if(defined('E_DEPRECATED')) error_reporting(E_ALL & ~(E_DEPRECATED | E_STRICT | E_NOTICE));
else error_reporting(E_ALL & ~E_NOTICE);
// If in live mode, ensure deprecation, strict and notices are not reported
if(Director::isLive()) {
error_reporting(E_ALL & ~(E_DEPRECATED | E_STRICT | E_NOTICE));
}
///////////////////////////////////////////////////////////////////////////////
// POST-MANIFEST COMMANDS

View File

@ -531,7 +531,7 @@ abstract class Object {
* @return array Numeric array of either {@link DataExtension} classnames,
* or eval'ed classname strings with constructor arguments.
*/
function get_extensions($class, $includeArgumentString = false) {
public static function get_extensions($class, $includeArgumentString = false) {
$extensions = Config::inst()->get($class, 'extensions');
if($includeArgumentString) {

View File

@ -796,7 +796,7 @@ class File extends DataObject {
* @param string $phpIniValue
* @return int
*/
public function ini2bytes($PHPiniValue) {
public static function ini2bytes($PHPiniValue) {
switch(strtolower(substr(trim($PHPiniValue), -1))) {
case 'g':
$PHPiniValue *= 1024;

View File

@ -298,39 +298,42 @@ class CompositeField extends FormField {
}
/**
* Return a readonly version of this field. Keeps the composition but returns readonly
* versions of all the children
* Return a readonly version of this field. Keeps the composition but returns readonly
* versions of all the child {@link FormField} objects.
*
* @return CompositeField
*/
public function performReadonlyTransformation() {
$newChildren = new FieldList();
$clone = clone $this;
foreach($clone->getChildren() as $idx => $child) {
if($clone->getChildren()) foreach($clone->getChildren() as $idx => $child) {
if(is_object($child)) $child = $child->transform(new ReadonlyTransformation());
$newChildren->push($child, $idx);
}
$clone->children = $newChildren;
$clone->readonly = true;
return $clone;
}
/**
* Return a readonly version of this field. Keeps the composition but returns readonly
* versions of all the children
* Return a disabled version of this field. Keeps the composition but returns disabled
* versions of all the child {@link FormField} objects.
*
* @return CompositeField
*/
public function performDisabledTransformation($trans) {
public function performDisabledTransformation() {
$newChildren = new FieldList();
$clone = clone $this;
if($clone->getChildren()) foreach($clone->getChildren() as $idx => $child) {
if(is_object($child)) {
$child = $child->transform($trans);
}
if(is_object($child)) $child = $child->transform(new DisabledTransformation());
$newChildren->push($child, $idx);
}
$clone->children = $newChildren;
$clone->readonly = true;
return $clone;
}
@ -394,15 +397,14 @@ class CompositeField extends FormField {
$result .= "</ul>";
return $result;
}
function validate($validator){
function validate($validator) {
$valid = true;
foreach($this->children as $idx => $child){
$valid = ($child && $child->validate($validator) && $valid);
}
return $valid;
}
}

View File

@ -216,8 +216,7 @@ class ConfirmedPasswordField extends FormField {
return (!$this->showOnClick || ($this->showOnClick && $isVisible && $isVisible->Value()));
}
function validate() {
$validator = $this->form->getValidator();
function validate($validator) {
$name = $this->name;
// if field isn't visible, don't validate

View File

@ -37,15 +37,9 @@ class CurrencyField extends TextField {
* Create a new class for this field
*/
function performReadonlyTransformation() {
$field = new CurrencyField_Readonly($this->name, $this->title, $this->value);
$field -> addExtraClass($this->extraClass());
return $field;
/*
$this is-a object and cant be passed as_a string of the first parameter of formfield constructor.
return new CurrencyField_Readonly($this);
*/
}
function validate($validator) {
@ -71,9 +65,8 @@ class CurrencyField_Readonly extends ReadonlyField{
if($this->value){
$val = $this->dontEscape ? $this->value : Convert::raw2xml($this->value);
$val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
}else {
$val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
} else {
$val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
}
$valforInput = $this->value ? Convert::raw2att($val) : "";
return "<span class=\"readonly ".$this->extraClass()."\" id=\"" . $this->id() . "\">$val</span><input type=\"hidden\" name=\"".$this->name."\" value=\"".$valforInput."\" />";
@ -104,9 +97,8 @@ class CurrencyField_Disabled extends CurrencyField{
if($this->value){
$val = $this->dontEscape ? $this->value : Convert::raw2xml($this->value);
$val = _t('CurrencyField.CURRENCYSYMBOL', '$') . number_format(preg_replace('/[^0-9.]/',"",$val), 2);
}else {
$val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
} else {
$val = '<i>'._t('CurrencyField.CURRENCYSYMBOL', '$').'0.00</i>';
}
$valforInput = $this->value ? Convert::raw2att($val) : "";
return "<input class=\"text\" type=\"text\" disabled=\"disabled\" name=\"".$this->name."\" value=\"".$valforInput."\" />";

View File

@ -19,18 +19,17 @@ class EmailField extends TextField {
* @param Validator $validator
* @return String
*/
function validate($validator){
function validate($validator) {
$this->value = trim($this->value);
$pcrePattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$';
$pcrePattern = '^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$';
// PHP uses forward slash (/) to delimit start/end of pattern, so it must be escaped
$pregSafePattern = str_replace('/', '\\/', $pcrePattern);
if($this->value && !preg_match('/' . $pregSafePattern . '/i', $this->value)){
$validator->validationError(
$this->name,
$validator->validationError(
$this->name,
_t('EmailField.VALIDATION', "Please enter an email address."),
"validation"
);

View File

@ -88,6 +88,30 @@ class FormField extends RequestHandler {
*/
protected $attributes = array();
/**
* Takes a fieldname and converts camelcase to spaced
* words. Also resolves combined fieldnames with dot syntax
* to spaced words.
*
* Examples:
* - 'TotalAmount' will return 'Total Amount'
* - 'Organisation.ZipCode' will return 'Organisation Zip Code'
*
* @param string $fieldName
* @return string
*/
public static function name_to_label($fieldName) {
if(strpos($fieldName, '.') !== false) {
$parts = explode('.', $fieldName);
$label = $parts[count($parts)-2] . ' ' . $parts[count($parts)-1];
} else {
$label = $fieldName;
}
$label = preg_replace("/([a-z]+)([A-Z])/","$1 $2", $label);
return $label;
}
/**
* Create a new field.
* @param name The internal field name, passed to forms.
@ -582,8 +606,8 @@ class FormField extends RequestHandler {
/**
* @return boolean
*/
function isDisabled() {
return $this->disabled;
function isDisabled() {
return $this->disabled;
}
/**
@ -591,8 +615,8 @@ class FormField extends RequestHandler {
* to actually transform this instance.
* @param $bool boolean Setting "false" has no effect on the field-state.
*/
function setDisabled($bool) {
$this->disabled = $bool;
function setDisabled($bool) {
$this->disabled = $bool;
return $this;
}
@ -607,21 +631,23 @@ class FormField extends RequestHandler {
}
/**
* Return a disabled version of this field
* Return a disabled version of this field.
* Tries to find a class of the class name of this field suffixed with "_Disabled",
* failing that, finds a method {@link setDisabled()}.
*
* @return FormField
*/
function performDisabledTransformation() {
$clone = clone $this;
$disabledClassName = $clone->class . '_Disabled';
if( ClassInfo::exists( $disabledClassName ) )
return new $disabledClassName( $this->name, $this->title, $this->value );
elseif($clone->hasMethod('setDisabled')){
if(ClassInfo::exists($disabledClassName)) {
return new $disabledClassName($this->name, $this->title, $this->value);
} else {
$clone->setDisabled(true);
return $clone;
}else{
return $this->performReadonlyTransformation();
}
}
function transform(FormTransformation $trans) {
return $trans->transform($this);
}
@ -662,14 +688,16 @@ class FormField extends RequestHandler {
if($content || $tag != 'input') return "<$tag$preparedAttributes>$content</$tag>";
else return "<$tag$preparedAttributes />";
}
/**
* Validation Functions for each field type by default
* formfield doesnt have a validation function
*
* @todo shouldn't this be an abstract method?
* Abstract method each {@link FormField} subclass must implement,
* determines whether the field is valid or not based on the value.
* @todo Make this abstract.
*
* @param Validator
* @return boolean
*/
function validate() {
function validate($validator) {
return true;
}
@ -690,7 +718,7 @@ class FormField extends RequestHandler {
*/
function setDescription($description) {
$this->description = $description;
return $this;
return $this;
}
/**
@ -721,31 +749,7 @@ class FormField extends RequestHandler {
return $validator->fieldIsRequired($this->name);
}
}
/**
* Takes a fieldname and converts camelcase to spaced
* words. Also resolves combined fieldnames with dot syntax
* to spaced words.
*
* Examples:
* - 'TotalAmount' will return 'Total Amount'
* - 'Organisation.ZipCode' will return 'Organisation Zip Code'
*
* @param string $fieldName
* @return string
*/
public function name_to_label($fieldName) {
if(strpos($fieldName, '.') !== false) {
$parts = explode('.', $fieldName);
$label = $parts[count($parts)-2] . ' ' . $parts[count($parts)-1];
} else {
$label = $fieldName;
}
$label = preg_replace("/([a-z]+)([A-Z])/","$1 $2", $label);
return $label;
}
/**
* Set the FieldList that contains this field.
*

View File

@ -89,12 +89,11 @@ class MemberDatetimeOptionsetField extends OptionsetField {
}
}
function validate() {
function validate($validator) {
$value = isset($_POST[$this->name . '_custom']) ? $_POST[$this->name . '_custom'] : null;
if(!$value) return true; // no custom value, don't validate
// Check that the current date with the date format is valid or not
$validator = $this->form ? $this->form->getValidator() : null;
require_once 'Zend/Date.php';
$date = Zend_Date::now()->toString($value);
$valid = Zend_Date::isDate($date, $value);
@ -107,4 +106,4 @@ class MemberDatetimeOptionsetField extends OptionsetField {
return false;
}
}
}
}

View File

@ -27,25 +27,6 @@ class SelectionGroup extends CompositeField {
Requirements::css(SAPPHIRE_DIR . '/css/SelectionGroup.css');
}
/**
* Return a readonly version of this field. Keeps the composition but returns readonly
* versions of all the children
*/
public function performDisabledTransformation($trans) {
$newChildren = array();
$clone = clone $this;
if($clone->children) foreach($clone->getChildren() as $idx => $child) {
if(is_object($child)) {
$child = $child->transform($trans);
}
$newChildren[$idx] = $child;
}
$clone->setChildren(new FieldList($newChildren));
$clone->setReadonly(true);
return $clone;
}
function FieldSet() {
return $this->FieldList();
@ -76,7 +57,7 @@ class SelectionGroup extends CompositeField {
if(is_object($item)) $newItems[] = $item->customise($extra);
else $newItems[] = new ArrayData($extra);
$firstSelected = $checked ="";
$firstSelected = $checked ="";
}
return new ArrayList($newItems);
}

View File

@ -129,7 +129,7 @@ class TabSet extends CompositeField {
/**
* Add a new child field to the end of the set.
*/
public function push($field) {
public function push(FormField $field) {
parent::push($field);
$field->setTabSet($this);
}
@ -155,4 +155,4 @@ class TabSet extends CompositeField {
public function removeByName( $tabName, $dataFieldOnly = false ) {
parent::removeByName( $tabName, $dataFieldOnly );
}
}
}

View File

@ -687,7 +687,7 @@ class TableField_Item extends TableListField_Item {
return new FieldList($this->fields);
}
function Fields() {
function Fields($xmlSafe = true) {
return $this->fields;
}

View File

@ -25,7 +25,7 @@ class Hierarchy extends DataExtension {
function augmentWrite(&$manipulation) {
}
static function add_to_class($class, $extensionClass, $args) {
static function add_to_class($class, $extensionClass, $args = null) {
Config::inst()->update($class, 'has_one', array('Parent' => $class));
parent::add_to_class($class, $extensionClass, $args);
}

View File

@ -107,7 +107,7 @@ class Versioned extends DataExtension {
'Version' => 'Int'
);
static function add_to_class($class, $extensionClass, $args) {
static function add_to_class($class, $extensionClass, $args = null) {
Config::inst()->update($class, 'has_many', array('Versions' => $class));
parent::add_to_class($class, $extensionClass, $args);
}

View File

@ -7,7 +7,7 @@
*
* Example with a combined street name and number:
* <code>
* class Street extends DBFields implements CompositeDBField() {
* class Street extends DBField implements CompositeDBField {
* protected $streetNumber;
* protected $streetName;
* protected $isChanged = false;
@ -42,7 +42,7 @@
* }
*
* function setValue($value, $record = null, $markChanged=true) {
* if ($value instanceof Street && $value->hasValue()) {
* if ($value instanceof Street && $value->exists()) {
* $this->setStreetName($value->getStreetName(), $markChanged);
* $this->setStreetNumber($value->getStreetNumber(), $markChanged);
* if($markChanged) $this->isChanged = true;
@ -85,7 +85,7 @@
* return $this->isChanged;
* }
*
* function hasValue() {
* function exists() {
* return ($this->getStreetName() || $this->getStreetNumber());
* }
* }
@ -179,4 +179,4 @@ interface CompositeDBField {
*/
function exists();
}
}

View File

@ -16,7 +16,7 @@
*/
class Time extends DBField {
function setValue($value) {
function setValue($value, $record = null) {
if($value) {
if(preg_match( '/(\d{1,2})[:.](\d{2})([a|A|p|P|][m|M])/', $value, $match )) $this->TwelveHour( $match );
else $this->value = date('H:i:s', strtotime($value));

View File

@ -75,7 +75,7 @@ class FulltextSearchable extends DataExtension {
parent::__construct();
}
static function add_to_class($class, $extensionClass, $args) {
static function add_to_class($class, $extensionClass, $args = null) {
Config::inst()->update($class, 'indexes', array('SearchFields' => array(
'type' => 'fulltext',
'name' => 'SearchFields',
@ -90,7 +90,7 @@ class FulltextSearchable extends DataExtension {
*
* @return Array
*/
function get_searchable_classes() {
static function get_searchable_classes() {
return self::$searchable_classes;
}

View File

@ -111,7 +111,7 @@ class ChangePasswordForm extends Form {
}
else {
// Redirect to default location - the login form saying "You are logged in as..."
$redirectURL = HTTP::setGetVar('BackURL', Director::absoluteBaseURL(), Security::Link('login'));
$redirectURL = HTTP::setGetVar('BackURL', Director::absoluteBaseURL(), $this->controller->Link('login'));
Director::redirect($redirectURL);
}
} else {

View File

@ -130,14 +130,14 @@ JS
if(isset($_REQUEST['BackURL'])) $backURL = $_REQUEST['BackURL'];
else $backURL = null;
if($backURL) Session::set('BackURL', $backURL);
if($backURL) Session::set('BackURL', $backURL);
if($badLoginURL = Session::get("BadLoginURL")) {
$this->controller->redirect($badLoginURL);
} else {
// Show the right tab on failed login
$loginLink = Director::absoluteURL(Security::Link("login"));
if($backURL) $loginLink .= '?BackURL=' . urlencode($backURL);
$loginLink = Director::absoluteURL($this->controller->Link('login'));
if($backURL) $loginLink .= '?BackURL=' . urlencode($backURL);
$this->controller->redirect($loginLink . '#' . $this->FormName() .'_tab');
}
}

View File

@ -281,7 +281,7 @@ class PermissionCheckboxSetField_Readonly extends PermissionCheckboxSetField {
protected $readonly = true;
function saveInto($record) {
function saveInto(DataObjectInterface $record) {
return false;
}
}

View File

@ -514,7 +514,9 @@ class Security extends Controller {
*/
public static function getPasswordResetLink($autoLoginHash) {
$autoLoginHash = urldecode($autoLoginHash);
return self::Link('changepassword') . "?h=$autoLoginHash";
$selfControllerClass = __CLASS__;
$selfController = new $selfControllerClass();
return $selfController->Link('changepassword') . "?h=$autoLoginHash";
}
/**

View File

@ -83,11 +83,11 @@ class MemberDatetimeOptionsetFieldTest extends SapphireTest {
function testDateFormValid() {
$field = new MemberDatetimeOptionsetField('DateFormat', 'DateFormat');
$this->assertTrue($field->validate());
$this->assertTrue($field->validate(null));
$_POST['DateFormat_custom'] = 'dd MM yyyy';
$this->assertTrue($field->validate());
$this->assertTrue($field->validate(null));
$_POST['DateFormat_custom'] = 'sdfdsfdfd1244';
$this->assertFalse($field->validate());
$this->assertFalse($field->validate(null));
}
}
@ -97,4 +97,4 @@ class MemberDatetimeOptionsetFieldTest_Controller extends Controller {
return 'test';
}
}
}

View File

@ -19,7 +19,7 @@ class SimpleTestCompatibility {
* @access public
* @static
*/
function copy($object) {
static function copy($object) {
if (version_compare(phpversion(), '5') >= 0) {
eval('$copy = clone $object;');
return $copy;
@ -170,4 +170,4 @@ class SimpleTestCompatibility {
}
}
}
?>
?>

View File

@ -120,7 +120,7 @@ class SimpleForm {
$class = $this->_encoding;
$encoding = new $class();
for ($i = 0, $count = count($this->_widgets); $i < $count; $i++) {
$this->_widgets[$i]->write($encoding);
$this->_widgets[$i]->write($encoding, 0, 0);
}
return $encoding;
}
@ -305,7 +305,7 @@ class SimpleForm {
foreach ($this->_buttons as $button) {
if ($selector->isMatch($button)) {
$encoding = $this->_encode();
$button->write($encoding);
$button->write($encoding, 0, 0);
if ($additional) {
$encoding->merge($additional);
}

View File

@ -980,4 +980,4 @@ class SimplePage {
return null;
}
}
?>

View File

@ -690,7 +690,7 @@ class SimpleHtmlSaxParser {
* @access public
* @static
*/
function decodeHtml($html) {
static function decodeHtml($html) {
return html_entity_decode($html, ENT_QUOTES);
}
@ -703,7 +703,7 @@ class SimpleHtmlSaxParser {
* @access public
* @static
*/
function normalise($html) {
static function normalise($html) {
$text = preg_replace('|<!--.*?-->|', '', $html);
$text = preg_replace('|<script[^>]*>.*?</script>|', '', $text);
$text = preg_replace('|<img[^>]*alt\s*=\s*"([^"]*)"[^>]*>|', ' \1 ', $text);
@ -761,4 +761,4 @@ class SimpleSaxListener {
function addContent($text) {
}
}
?>

View File

@ -325,7 +325,7 @@ class SimpleWidget extends SimpleTag {
* @param SimpleEncoding $encoding Form packet.
* @access public
*/
function write(&$encoding) {
function write(&$encoding, $x, $y) {
if ($this->getName()) {
$encoding->add($this->getName(), $this->getValue());
}
@ -680,7 +680,7 @@ class SimpleUploadTag extends SimpleWidget {
* @param SimpleEncoding $encoding Form packet.
* @access public
*/
function write(&$encoding) {
function write(&$encoding, $x, $y) {
if (! file_exists($this->getValue())) {
return;
}
@ -1415,4 +1415,4 @@ class SimpleFrameTag extends SimpleTag {
return false;
}
}
?>