Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Apr 17, 2023
1 parent 1c1616a commit e329785
Show file tree
Hide file tree
Showing 28 changed files with 100 additions and 130 deletions.
2 changes: 1 addition & 1 deletion src/EscapesValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait EscapesValues
/**
* Prepare a value to be escaped.
*/
public function escape(string $value = null, string $ignore = '', int $flags = 0): EscapedValue
public function escape(mixed $value = null, string $ignore = '', int $flags = 0): EscapedValue
{
return new EscapedValue($value, $ignore, $flags);
}
Expand Down
6 changes: 2 additions & 4 deletions src/Ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function setOption(int $option, mixed $value): bool
/**
* {@inheritdoc}
*/
public function getOption(int $option, mixed &$value = null): bool
public function getOption(int $option, mixed &$value = null): mixed
{
ldap_get_option($this->connection, $option, $value);

Expand Down Expand Up @@ -250,9 +250,7 @@ public function read(string $dn, string $filter, array $fields, bool $onlyAttrib
*/
public function parseResult(mixed $result, int &$errorCode = 0, string &$dn = null, string &$errorMessage = null, array &$referrals = null, array &$controls = null): LdapResultResponse|false
{
$success = ldap_parse_result($this->connection, $result, $errorCode);

if ($success) {
if (ldap_parse_result($this->connection, $result, $errorCode, $dn, $errorMessage, $referrals, $controls)) {
return new LdapResultResponse(
$errorCode, $dn, $errorMessage, $referrals, $controls
);
Expand Down
4 changes: 2 additions & 2 deletions src/LdapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function setRebindCallback(callable $callback): bool;
*
* @see https://www.php.net/manual/en/function.ldap-get-option.php
*/
public function getOption(int $option, mixed &$value = null): bool;
public function getOption(int $option, mixed &$value = null): mixed;

/**
* Starts a connection using TLS.
Expand Down Expand Up @@ -288,7 +288,7 @@ public function read(string $dn, string $filter, array $fields, bool $onlyAttrib
*
* @param \LDAP\Result $result
*/
public function parseResult(mixed $result, int &$errorCode, string &$dn = null, string &$errorMessage = null, array &$referrals = null, array &$controls = null): LdapResultResponse|false;
public function parseResult(mixed $result, int &$errorCode = 0, string &$dn = null, string &$errorMessage = null, array &$referrals = null, array &$controls = null): LdapResultResponse|false;

/**
* Binds to the current connection using the specified username and password.
Expand Down
6 changes: 4 additions & 2 deletions src/Models/ActiveDirectory/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function getObjectSid(): ?string
public function getConvertedSid($sid = null): ?string
{
try {
return (string) $this->newObjectSid(
$sid ?? $this->getObjectSid()
return $this->newObjectSid(
(string) ($sid ?? $this->getObjectSid())
);
} catch (InvalidArgumentException) {
return null;
Expand Down Expand Up @@ -123,6 +123,8 @@ public function restore(string $newParentDn = null): bool
$this->setRawAttribute('distinguishedname', $newDn);

$this->save(['isDeleted' => null]);

return true;
}

/**
Expand Down
50 changes: 14 additions & 36 deletions src/Models/Attributes/EscapedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,33 @@ class EscapedValue
{
/**
* The value to be escaped.
*
* @var string
*/
protected $value;
protected mixed $value;

/**
* The characters to ignore when escaping.
*
* @var string
*/
protected $ignore;
protected string $ignore;

/**
* The escape flags.
*
* @var int
*/
protected $flags;
protected int $flags;

/**
* Constructor.
*
* @param string $value
* @param string $ignore
* @param int $flags
*/
public function __construct($value, $ignore = '', $flags = 0)
public function __construct(mixed $value, string $ignore = '', int $flags = 0)
{
$this->value = (string) $value;
$this->value = $value;
$this->ignore = $ignore;
$this->flags = $flags;
}

/**
* Un-escapes a hexadecimal string into its original string representation.
*
* @param string $value
* @return string
*/
public static function unescape($value)
public static function unescape(string $value): string
{
return preg_replace_callback('/\\\([0-9A-Fa-f]{2})/', function ($matches) {
return chr(hexdec($matches[1]));
Expand All @@ -57,32 +44,29 @@ public static function unescape($value)
*/
public function __toString(): string
{
return (string) $this->get();
return $this->get();
}

/**
* Get the escaped value.
*/
public function get()
public function get(): string
{
return ldap_escape($this->value, $this->ignore, $this->flags);
return ldap_escape((string) $this->value, $this->ignore, $this->flags);
}

/**
* Get the raw (unescaped) value.
*/
public function raw()
public function raw(): mixed
{
return $this->value;
}

/**
* Set the characters to exclude from being escaped.
*
* @param string $characters
* @return $this
*/
public function ignore($characters)
public function ignore(string $characters): static
{
$this->ignore = $characters;

Expand All @@ -91,10 +75,8 @@ public function ignore($characters)

/**
* Prepare the value to be escaped for use in a distinguished name.
*
* @return $this
*/
public function dn()
public function dn(): static
{
$this->flags = LDAP_ESCAPE_DN;

Expand All @@ -103,10 +85,8 @@ public function dn()

/**
* Prepare the value to be escaped for use in a filter.
*
* @return $this
*/
public function filter()
public function filter(): static
{
$this->flags = LDAP_ESCAPE_FILTER;

Expand All @@ -115,10 +95,8 @@ public function filter()

/**
* Prepare the value to be escaped for use in a distinguished name and filter.
*
* @return $this
*/
public function both()
public function both(): static
{
$this->flags = LDAP_ESCAPE_FILTER + LDAP_ESCAPE_DN;

Expand Down
28 changes: 8 additions & 20 deletions src/Models/Attributes/Sid.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,23 @@ class Sid
{
/**
* The string SID value.
*
* @var string
*/
protected $value;
protected string $value;

/**
* Determines if the specified SID is valid.
*
* @param string $sid
* @return bool
*/
public static function isValid($sid)
public static function isValid(string $sid): bool
{
return (bool) preg_match("/^S-\d(-\d{1,10}){1,16}$/i", (string) $sid);
return (bool) preg_match("/^S-\d(-\d{1,10}){1,16}$/i", $sid);
}

/**
* Constructor.
*
* @throws InvalidArgumentException
*/
public function __construct($value)
public function __construct(string $value)
{
if (static::isValid($value)) {
$this->value = $value;
Expand All @@ -50,20 +45,16 @@ public function __toString(): string

/**
* Returns the string value of the SID.
*
* @return string
*/
public function getValue()
public function getValue(): string
{
return $this->value;
}

/**
* Returns the binary variant of the SID.
*
* @return string
*/
public function getBinary()
public function getBinary(): string
{
$sid = explode('-', ltrim($this->value, 'S-'));

Expand All @@ -83,11 +74,8 @@ public function getBinary()

/**
* Returns the string variant of a binary SID.
*
* @param string $binary
* @return string|null
*/
protected function binarySidToString($binary)
protected function binarySidToString(string $binary): string
{
// Revision - 8bit unsigned int (C1)
// Count - 8bit unsigned int (C1)
Expand All @@ -96,7 +84,7 @@ protected function binarySidToString($binary)
$sid = @unpack('C1rev/C1count/x2/N1id', $binary);

if (! isset($sid['id']) || ! isset($sid['rev'])) {
return;
return '';
}

$revisionLevel = $sid['rev'];
Expand Down
4 changes: 2 additions & 2 deletions src/Models/BatchModification.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getOriginal(): array
/**
* Set the attribute of the modification.
*/
public function setAttribute(string $attribute): static
public function setAttribute(string $attribute = null): static
{
$this->attribute = $attribute;

Expand Down Expand Up @@ -138,7 +138,7 @@ public function setType(int $type = null): static
/**
* Returns the type of the modification.
*/
public function getType(): int
public function getType(): ?int
{
return $this->type;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected static function withoutEvents(Closure $callback)
$dispatcher = $container->getDispatcher();

if ($dispatcher) {
$container->setEventDispatcher(
$container->setDispatcher(
new NullDispatcher($dispatcher)
);
}
Expand All @@ -30,7 +30,7 @@ protected static function withoutEvents(Closure $callback)
return $callback();
} finally {
if ($dispatcher) {
$container->setEventDispatcher($dispatcher);
$container->setDispatcher($dispatcher);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function getDn(): ?string
/**
* Set the models distinguished name.
*/
public function setDn(string $dn): static
public function setDn(string $dn = null): static
{
$this->dn = $dn;

Expand Down Expand Up @@ -1073,7 +1073,7 @@ protected function deleteLeafNodes(): void
* @throws ModelDoesNotExistException
* @throws \LdapRecord\LdapRecordException
*/
public function deleteAttribute(array $attributes): void
public function deleteAttribute(array|string $attributes): void
{
$this->assertExists();

Expand Down
4 changes: 2 additions & 2 deletions src/Query/ArrayCacheStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ public function set(string $key, mixed $value, null|int|\DateInterval $ttl = nul
/**
* Get the expiration time of the key.
*/
protected function calculateExpiration(int $seconds): int
protected function calculateExpiration(int $seconds = null): int
{
return $this->toTimestamp($seconds);
}

/**
* Get the UNIX timestamp for the given number of seconds.
*/
protected function toTimestamp(int $seconds): int
protected function toTimestamp(int $seconds = null): int
{
return $seconds > 0 ? $this->availableAt($seconds) : 0;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function getType(): string
/**
* Set the base distinguished name of the query.
*/
public function setBaseDn(Model|string $dn): static
public function setBaseDn(Model|string $dn = null): static
{
$this->baseDn = $this->substituteBaseInDn($dn);

Expand Down Expand Up @@ -302,7 +302,7 @@ public function setDn(Model|string|null $dn = null): static
/**
* Substitute the base DN string template for the current base.
*/
protected function substituteBaseInDn(Model|string $dn): string
protected function substituteBaseInDn(Model|string $dn = null): string
{
return str_replace(
'{base}',
Expand Down Expand Up @@ -552,7 +552,7 @@ public function run(string $filter): mixed
}

return $ldap->{$this->type}(
$this->dn ?? $this->baseDn,
(string) ($this->dn ?? $this->baseDn),
$filter,
$this->getSelects(),
$onlyAttributes = false,
Expand Down Expand Up @@ -902,7 +902,7 @@ public function notFilter(Closure $closure): static
*
* @throws InvalidArgumentException
*/
public function where(array|string $field, string $operator = null, string $value = null, string $boolean = 'and', bool $raw = false): static
public function where(array|string $field, mixed $operator = null, mixed $value = null, string $boolean = 'and', bool $raw = false): static
{
if (is_array($field)) {
// If the field is an array, we will assume we have been
Expand Down Expand Up @@ -935,7 +935,7 @@ public function where(array|string $field, string $operator = null, string $valu
/**
* Prepare the value for being queried.
*/
protected function prepareWhereValue(string $field, string $value = null, bool $raw = false): string
protected function prepareWhereValue(string $field, mixed $value = null, bool $raw = false): string
{
return $raw ? $value : $this->escape($value);
}
Expand All @@ -945,7 +945,7 @@ protected function prepareWhereValue(string $field, string $value = null, bool $
*
* Values given to this method are not escaped.
*/
public function whereRaw(array|string $field, string $operator = null, string $value = null): static
public function whereRaw(array|string $field, string $operator = null, mixed $value = null): static
{
return $this->where($field, $operator, $value, 'and', true);
}
Expand Down
Loading

0 comments on commit e329785

Please sign in to comment.