Commit 18ac0152 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

feat(Attribute) Add strict typing to Attribute methods

issue #5956
parent 9375c2a4
......@@ -134,7 +134,7 @@ class SnapshotsAttribute extends OrderedArrayAttribute
$this->setInLdap(FALSE);
}
function setParent (&$plugin)
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if (is_object($this->plugin)) {
......
......@@ -51,7 +51,7 @@ class BaseSelectorAttribute extends Attribute
trigger_error('method setManagedAttributes is not supported for BaseSelectorAttribute');
}
function setParent (&$plugin)
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if (is_object($this->plugin)) {
......@@ -147,7 +147,7 @@ class BaseSelectorAttribute extends Attribute
return $this->value;
}
function renderFormInput ()
function renderFormInput (): string
{
$smarty = get_smarty();
$smarty->assign('usePrototype', 'true');
......@@ -164,7 +164,7 @@ class BaseSelectorAttribute extends Attribute
return $this->renderAcl($display);
}
function getHtmlId ()
function getHtmlId (): string
{
if (isset($this->baseSelector)) {
return $this->baseSelector->getInputHtmlId();
......@@ -173,7 +173,7 @@ class BaseSelectorAttribute extends Attribute
}
}
function serializeAttribute (array &$attributes, $form = TRUE)
function serializeAttribute (array &$attributes, bool $form = TRUE)
{
if (!$form || $this->visible) {
parent::serializeAttribute($attributes, $form);
......
......@@ -90,7 +90,7 @@ class BooleanAttribute extends Attribute
}
}
function displayValue ($value)
function displayValue ($value): string
{
if ($this->isTemplate() && $this->isTemplatable() && ($value === '%askme%')) {
return $value;
......@@ -99,7 +99,7 @@ class BooleanAttribute extends Attribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$attributes = ($this->value ? ['checked' => 'checked'] : []);
......@@ -114,7 +114,7 @@ class BooleanAttribute extends Attribute
return $this->renderAcl($display);
}
function renderTemplateInput ()
function renderTemplateInput (): string
{
if ($this->isTemplatable()) {
$id = $this->getHtmlId();
......@@ -138,7 +138,7 @@ class BooleanAttribute extends Attribute
}
}
protected function managedAttributesJS ()
protected function managedAttributesJS (): string
{
$js = '';
$id = $this->getHtmlId();
......
......@@ -40,14 +40,14 @@ class CompositeAttribute extends Attribute
* \param string $readFormat the preg_match format that's gonna be used in order to read values from LDAP
* \param string $writeFormat the printf format that's gonna be used in order to write values into LDAP
* \param string $acl The name of the acl for this attribute if he does not use its own. (Leave empty if he should use its own like most attributes do)
* \param string $label The label to show for this attribute. Only useful if you put this attribute inside a SetAttribute, or if you use a specific template that needs it.
* \param ?string $label The label to show for this attribute. Only useful if you put this attribute inside a SetAttribute, or if you use a specific template that needs it.
*/
function __construct ($description, $ldapName, array $attributes, $readFormat, $writeFormat, $acl = "", $label = NULL)
function __construct (string $description, string $ldapName, array $attributes, $readFormat, $writeFormat, string $acl = '', string $label = NULL)
{
if ($label === NULL) {
$label = $ldapName;
}
parent::__construct($label, $description, $ldapName, FALSE, "", $acl);
parent::__construct($label, $description, $ldapName, FALSE, '', $acl);
$this->readFormat = $readFormat;
$this->writeFormat = $writeFormat;
$this->setAttributes($attributes);
......@@ -62,7 +62,7 @@ class CompositeAttribute extends Attribute
unset($attribute);
}
function setIsSubAttribute ($bool)
function setIsSubAttribute (bool $bool)
{
parent::setIsSubAttribute($bool);
foreach ($this->attributes as &$attribute) {
......@@ -71,7 +71,7 @@ class CompositeAttribute extends Attribute
unset($attribute);
}
function setAcl ($acl)
function setAcl (string $acl)
{
parent::setAcl($acl);
foreach ($this->attributes as &$attribute) {
......@@ -80,7 +80,7 @@ class CompositeAttribute extends Attribute
unset($attribute);
}
function setParent (&$plugin)
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
foreach ($this->attributes as &$attribute) {
......@@ -94,12 +94,12 @@ class CompositeAttribute extends Attribute
trigger_error('method setManagedAttributes is not supported for CompositeAttribute');
}
function setLinearRendering ($bool)
function setLinearRendering (bool $bool)
{
$this->linearRendering = $bool;
}
function readValues ($value)
function readValues (string $value): array
{
$res = preg_match($this->readFormat, $value, $m);
if ($res === 1) {
......@@ -195,7 +195,7 @@ class CompositeAttribute extends Attribute
return $this->writeValues($values);
}
function getArrayValue ()
function getArrayValue (): array
{
return array_map(
function ($a)
......@@ -221,7 +221,7 @@ class CompositeAttribute extends Attribute
unset($attribute);
}
function renderAttribute (array &$attributes, $readOnly)
function renderAttribute (array &$attributes, bool $readOnly)
{
if ($this->visible) {
if ($this->linearRendering) {
......@@ -236,7 +236,7 @@ class CompositeAttribute extends Attribute
}
}
function getForHtmlId ()
function getForHtmlId (): string
{
// Label (if any) should point to the first attribute
if (isset($this->attributes[0])) {
......@@ -246,7 +246,7 @@ class CompositeAttribute extends Attribute
}
}
function serializeAttribute (array &$attributes, $form = TRUE)
function serializeAttribute (array &$attributes, bool $form = TRUE)
{
if ($form) {
if ($this->visible) {
......@@ -269,7 +269,7 @@ class CompositeAttribute extends Attribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$display = "";
foreach ($this->attributes as &$attribute) {
......@@ -282,7 +282,7 @@ class CompositeAttribute extends Attribute
return $display;
}
public function htmlIds ()
public function htmlIds (): array
{
$ret = [];
foreach ($this->attributes as &$attribute) {
......
......@@ -150,7 +150,7 @@ class DateAttribute extends Attribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$attributes = [
'value' => '{literal}'.$this->getValue().'{/literal}',
......@@ -223,7 +223,7 @@ class TimeHisAttribute extends CompositeAttribute
$this->setLinearRendering(TRUE);
}
function readValues ($value)
function readValues (string $value): array
{
$values = parent::readValues($value);
if ($this->convert) {
......@@ -253,7 +253,7 @@ class TimeHisAttribute extends CompositeAttribute
return parent::writeValues($values);
}
function displayValue ($value)
function displayValue ($value): string
{
$values = parent::readValues($value);
$datetime = new DateTime('T'.implode(':', $values), timezone::utc());
......@@ -327,7 +327,7 @@ class GeneralizedTimeDisplayAttribute extends GeneralizedTimeDateAttribute
return $this->computeLdapValue();
}
function renderFormInput ()
function renderFormInput (): string
{
$date = $this->getDateValue();
$date->setTimezone(timezone::getDefaultTimeZone());
......
......@@ -62,14 +62,14 @@ class FileAttribute extends Attribute
@fclose($handle);
}
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$display = $this->renderInputField('file', $id);
return $this->renderAcl($display);
}
function displayValue ($value)
function displayValue ($value): string
{
return sprintf(_('%s (%d bytes)'), $this->getLabel(), mb_strlen($value, '8bit'));
}
......@@ -79,7 +79,7 @@ class FileAttribute extends Attribute
* \param array &$attributes the attributes array
* \param boolean $form
*/
function serializeAttribute (array &$attributes, $form = TRUE)
function serializeAttribute (array &$attributes, bool $form = TRUE)
{
if (!$form || $this->visible) {
parent::serializeAttribute($attributes, $form);
......@@ -166,7 +166,7 @@ class FileDownloadAttribute extends FileAttribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$display = '';
......@@ -188,7 +188,7 @@ class FileDownloadAttribute extends FileAttribute
return $this->renderAcl($display);
}
public function htmlIds ()
public function htmlIds (): array
{
$id = $this->getHtmlId();
$ids = [];
......@@ -202,7 +202,7 @@ class FileDownloadAttribute extends FileAttribute
return $ids;
}
function renderAttribute (array &$attributes, $readOnly)
function renderAttribute (array &$attributes, bool $readOnly)
{
if ($this->upload === FALSE) {
parent::renderAttribute($attributes, FALSE);
......@@ -263,7 +263,7 @@ class FileTextAreaAttribute extends FileDownloadAttribute
return str_replace(["\r\n", "\r"], "\n", $value);
}
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$display = '<textarea name="'.$id.'_text" id="'.$id.'_text"'.
......@@ -272,7 +272,7 @@ class FileTextAreaAttribute extends FileDownloadAttribute
return $this->renderAcl($display).parent::renderFormInput();
}
public function htmlIds ()
public function htmlIds (): array
{
$id = $this->getHtmlId();
$ids = parent::htmlIds();
......@@ -372,7 +372,7 @@ class ImageAttribute extends FileAttribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$this->setValue($this->inputValue($this->getValue()));
$id = $this->getHtmlId();
......@@ -415,7 +415,7 @@ class ImageAttribute extends FileAttribute
return $this->renderAcl($display);
}
public function htmlIds ()
public function htmlIds (): array
{
$id = $this->getHtmlId();
return [$id.'_img',$id,'upload'.$id];
......
......@@ -38,7 +38,7 @@ class FlagsAttribute extends StringAttribute
$this->flags = $flags;
}
function setParent (&$plugin)
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if (is_object($this->plugin)) {
......
......@@ -89,7 +89,7 @@ class IntAttribute extends Attribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$attributes = [
......@@ -117,7 +117,7 @@ class IntAttribute extends Attribute
return $this->renderAcl($display);
}
function renderTemplateInput ()
function renderTemplateInput (): string
{
$id = $this->getHtmlId();
$attributes = [
......
......@@ -136,7 +136,7 @@ class PhoneNumberButtonAttribute extends PhoneNumberAttribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$display = parent::renderFormInput();
......@@ -154,7 +154,7 @@ class PhoneNumberButtonAttribute extends PhoneNumberAttribute
return $display;
}
public function htmlIds ()
public function htmlIds (): array
{
$ids = parent::htmlIds();
if ($this->type !== FALSE) {
......
......@@ -112,7 +112,7 @@ class SelectAttribute extends Attribute
return $this->outputs;
}
function setRequired ($bool)
function setRequired (bool $bool)
{
parent::setRequired($bool);
$key = array_search("", $this->choices, TRUE);
......@@ -129,7 +129,7 @@ class SelectAttribute extends Attribute
}
}
function displayValue ($value)
function displayValue ($value): string
{
if ($this->outputs !== NULL) {
if (isset($this->outputs[$value])) {
......@@ -155,7 +155,7 @@ class SelectAttribute extends Attribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$smarty = get_smarty();
$id = $this->getHtmlId();
......@@ -191,7 +191,7 @@ class SelectAttribute extends Attribute
return $this->renderAcl($display);
}
function serializeAttribute (array &$attributes, $form = TRUE)
function serializeAttribute (array &$attributes, bool $form = TRUE)
{
if (!$form || $this->visible) {
parent::serializeAttribute($attributes, $form);
......@@ -211,7 +211,7 @@ class SelectAttribute extends Attribute
$this->size = $size;
}
function setParent (&$plugin)
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if ($this->isTemplate() && !in_array('%askme%', $this->choices)) {
......@@ -240,7 +240,7 @@ class ObjectSelectAttribute extends SelectAttribute
$this->objectFilter = $objectFilter;
}
function setParent (&$plugin)
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if (is_object($this->plugin)) {
......
......@@ -35,7 +35,7 @@ class SetAttribute extends Attribute
* \param array $values The default values
* \param boolean $valueUnicity Should the value unicity be checked
*/
function __construct ($attribute, $values = [], $valueUnicity = TRUE)
function __construct (Attribute $attribute, array $values = [], bool $valueUnicity = TRUE)
{
parent::__construct(
$attribute->getLabel(), $attribute->getDescription(),
......@@ -48,7 +48,7 @@ class SetAttribute extends Attribute
$this->valueUnicity = $valueUnicity;
}
function setIsSubAttribute ($bool)
function setIsSubAttribute (bool $bool)
{
parent::setIsSubAttribute($bool);
$this->attribute->setIsSubAttribute($this->isSubAttribute);
......@@ -59,7 +59,7 @@ class SetAttribute extends Attribute
trigger_error('method setManagedAttributes is not supported for SetAttributes');
}
function setLinearRendering ($bool)
function setLinearRendering (bool $bool)
{
$this->linearRendering = $bool;
}
......@@ -74,7 +74,7 @@ class SetAttribute extends Attribute
}
}
function getAcl ()
function getAcl (): string
{
if ($this->attribute === FALSE) {
return parent::getAcl();
......@@ -90,7 +90,7 @@ class SetAttribute extends Attribute
return $this->attribute->getAclInfo();
}
function setAcl ($acl)
function setAcl (string $acl)
{
if ($this->attribute === FALSE) {
return parent::setAcl($acl);
......@@ -158,7 +158,7 @@ class SetAttribute extends Attribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$display = $this->renderOnlyFormInput();
$attr_display = $this->renderAttributeInput(FALSE);
......@@ -166,7 +166,7 @@ class SetAttribute extends Attribute
return $this->renderAcl($display).$attr_display.$this->renderAcl($buttons);
}
function renderTemplateInput ()
function renderTemplateInput (): string
{
$display = $this->renderOnlyFormInput();
$attr_display = $this->renderAttributeInput(TRUE);
......@@ -174,7 +174,7 @@ class SetAttribute extends Attribute
return $this->renderAcl($display).$attr_display.$this->renderAcl($buttons);
}
function renderOnlyFormInput ()
function renderOnlyFormInput (): string
{
if (($this->size < 15) && ($this->size < count($this->value))) {
$this->size = min(15, count($this->value));
......@@ -227,7 +227,7 @@ class SetAttribute extends Attribute
}
}
function renderAttribute (array &$attributes, $readOnly)
function renderAttribute (array &$attributes, bool $readOnly)
{
if ($this->attribute === FALSE) {
return parent::renderAttribute($attributes, $readOnly);
......@@ -259,7 +259,7 @@ class SetAttribute extends Attribute
}
}
function serializeAttribute (array &$attributes, $form = TRUE)
function serializeAttribute (array &$attributes, bool $form = TRUE)
{
parent::serializeAttribute($attributes, $form);
if ($this->attribute === FALSE) {
......@@ -276,7 +276,7 @@ class SetAttribute extends Attribute
}
}
function getForHtmlId ()
function getForHtmlId (): string
{
// Label should point to the attribute
if (is_object($this->attribute)) {
......@@ -299,7 +299,7 @@ class SetAttribute extends Attribute
return array_values($this->value);
}
public function htmlIds ()
public function htmlIds (): array
{
$id = $this->getHtmlId();
return array_merge(['add'.$id,'del'.$id,'row'.$id], $this->attribute->htmlIds());
......@@ -309,7 +309,7 @@ class SetAttribute extends Attribute
*
* \param simplePlugin &$plugin The parent plugin
*/
function setParent (&$plugin)
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if ($this->attribute !== FALSE) {
......@@ -355,7 +355,7 @@ class SetAttribute extends Attribute
unset($value);
}
function foreignKeyCheck ($value, array $source)
function foreignKeyCheck ($value, array $source): bool
{
return in_array($value, $this->value);
}
......@@ -446,7 +446,7 @@ class OrderedArrayAttribute extends SetAttribute
}
}
function renderOnlyFormInput ()
function renderOnlyFormInput (): string
{
if (($this->size < 15) && ($this->size < count($this->value))) {
$this->size = min(15, count($this->value));
......@@ -642,7 +642,7 @@ class OrderedArrayAttribute extends SetAttribute
}
}
public function htmlIds ()
public function htmlIds (): array
{
$id = $this->getHtmlId();
$ids = ['add'.$id];
......
......@@ -66,7 +66,7 @@ class StringAttribute extends Attribute
}
}
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$attributes = [
......@@ -91,7 +91,7 @@ class StringAttribute extends Attribute
return $this->renderAcl($display);
}
function renderTemplateInput ()
function renderTemplateInput (): string
{
$id = $this->getHtmlId();
$attributes = [
......@@ -172,7 +172,7 @@ class TrimmedStringAttribute extends StringAttribute
*/
class TextAreaAttribute extends StringAttribute
{
function renderFormInput ()
function renderFormInput (): string
{
$id = $this->getHtmlId();
$display = '<textarea name="'.$id.'" id="'.$id.'"'.
......@@ -183,7 +183,7 @@ class TextAreaAttribute extends StringAttribute
return $this->renderAcl($display);
}
function renderTemplateInput ()
function renderTemplateInput (): string
{
$id = $this->getHtmlId();
$display = '<textarea name="'.$id.'" id="'.$id.'"'.
......@@ -203,7 +203,7 @@ class PasswordAttribute extends StringAttribute
protected $autocomplete = FALSE;
protected $inputType = 'password';
function renderFormInput ()
function renderFormInput (): string
{
$display = parent::renderFormInput();
if ($this->autocomplete === FALSE) {
......
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2016 FusionDirectory
Copyright (C) 2012-2019 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -97,7 +98,7 @@ class Attribute
* \param mixed $defaultValue The default value for this attribute
* \param string $acl The name of the acl for this attribute if he does not use its own. (Leave empty if he should use its own like most attributes do)
*/
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = "", $acl = "")
function __construct (string $label, string $description, string $ldapName, bool $required = FALSE, $defaultValue = '', string $acl = '')
{
$this->label = $label;
$this->description = $description;
......@@ -114,38 +115,38 @@ class Attribute
*