Introduce PHP7 strict typing

This commit is contained in:
Diogo Cordeiro
2019-11-02 23:36:21 +00:00
parent 79239e8e2d
commit 57f33b4328
14 changed files with 586 additions and 547 deletions

View File

@@ -1,5 +1,4 @@
<?php
/**
* XMPPHP: The PHP XMPP Library
* Copyright (C) 2008 Nathanael C. Fritz
@@ -49,70 +48,67 @@ class Roster
* Contains array with potentially two indexes 'contact' and 'presence'
* @var array
*/
protected $roster_array = array();
protected $roster_array = [];
/**
* Constructor
* @param array $roster_array
*/
public function __construct($roster_array = array())
public function __construct($roster_array = [])
{
if ($this->verifyRoster($roster_array)) {
$this->roster_array = $roster_array; //Allow for pre-population with existing roster
} else {
$this->roster_array = array();
$this->roster_array = [];
}
}
/**
*
* Check that a given roster array is of a valid structure (empty is still valid)
*
* @param array $roster_array
* @return bool
* @return bool true for valid, false otherwise
*/
protected function verifyRoster($roster_array)
protected function verifyRoster(array $roster_array): bool
{
#TODO once we know *what* a valid roster array looks like
return true;
}
/**
*
* Retrieve contact via jid
*
* @param string $jid
* @return mixed
* @return mixed|void
*/
public function getContact($jid)
public function getContact(string $jid)
{
if ($this->isContact($jid)) {
return $this->roster_array[$jid]['contact'];
}
return;
}
/**
*
* Discover if a contact exists in the roster via jid
*
* @param string $jid
* @return bool
*/
public function isContact($jid)
public function isContact(string $jid): bool
{
return (array_key_exists($jid, $this->roster_array));
}
/**
*
* Set presence
*
* @param string $presence
* @param integer $priority
* @param int $priority
* @param string $show
* @param string $status
*/
public function setPresence($presence, $priority, $show, $status)
public function setPresence(string $presence, int $priority, string $show, string $status): void
{
$presence = explode('/', $presence, 2);
$jid = $presence[0];
@@ -121,7 +117,7 @@ class Roster
if (!$this->isContact($jid)) {
$this->addContact($jid, 'not-in-roster');
}
$this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
$this->roster_array[$jid]['presence'][$resource] = ['priority' => $priority, 'show' => $show, 'status' => $status];
} else { //Nuke unavailable resources to save memory
unset($this->roster_array[$jid]['resource'][$resource]);
unset($this->roster_array[$jid]['presence'][$resource]);
@@ -129,54 +125,64 @@ class Roster
}
/**
*
* Add given contact to roster
*
* @param string $jid
* @param string $subscription
* @param string $name
* @param array $groups
* @param string $name (optional)
* @param array $groups (optional)
*/
public function addContact($jid, $subscription, $name = '', $groups = array())
public function addContact(string $jid, string $subscription, string $name = '', array $groups = []): void
{
$contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups);
$contact = ['jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups];
if ($this->isContact($jid)) {
$this->roster_array[$jid]['contact'] = $contact;
} else {
$this->roster_array[$jid] = array('contact' => $contact);
$this->roster_array[$jid] = ['contact' => $contact];
}
}
/*
*
* Return best presence for jid
/**
* Get presence
*
* @param string $jid
* @return array best presence for jid
*/
public function getPresence($jid)
public function getPresence(string $jid): array
{
$split = explode('/', $jid, 2);
$jid = $split[0];
if ($this->isContact($jid)) {
$current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
$current = [
'resource' => '',
'active' => '',
'priority' => -129, //Priorities can only be -128 = 127
'show' => '',
'status' => ''
];
foreach ($this->roster_array[$jid]['presence'] as $resource => $presence) {
//Highest available priority or just highest priority
if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
if ($presence['priority'] > $current['priority'] and
(
($presence['show'] == "chat" or $presence['show'] == "available") or
($current['show'] != "chat" or $current['show'] != "available")
)
) {
$current = $presence;
$current['resource'] = $resource;
}
}
return $current;
}
return [];
}
/**
*
* Get roster
*
* @return array roster_array
*/
public function getRoster()
public function getRoster(): array
{
return $this->roster_array;
}