Skip to content

Commit

Permalink
Add strict types, missing types and fix some CS
Browse files Browse the repository at this point in the history
  • Loading branch information
landrok committed May 11, 2021
1 parent a5c25f3 commit 7aca2db
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 167 deletions.
61 changes: 27 additions & 34 deletions src/ActivityPhp/Server.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ActivityPhp package.
*
Expand All @@ -16,47 +18,48 @@
use ActivityPhp\Server\Actor\Outbox;
use ActivityPhp\Server\Cache\CacheHelper;
use ActivityPhp\Server\Configuration;
use Exception;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;

/**
* \ActivityPhp\Server is the entry point for server processes.
*/
class Server
*/
final class Server
{
/**
* @var self
*/
private static $singleton;

/**
* @var \ActivityPhp\Server\Actor[]
* @var array<\ActivityPhp\Server\Actor>
*/
protected $actors = [];
private $actors = [];

/**
* @var \ActivityPhp\Server\Actor\Inbox[]
* @var array<\ActivityPhp\Server\Actor\Inbox>
*/
protected $inboxes = [];
private $inboxes = [];

/**
* @var \ActivityPhp\Server\Actor\Outbox[]
* @var array<\ActivityPhp\Server\Actor\Outbox>
*/
protected $outboxes = [];
private $outboxes = [];

/**
* @var null|\Psr\Log\LoggerInterface
* @var \Psr\Log\LoggerInterface|null
*/
protected $logger;
private $logger;

/**
* @var null|\ActivityPhp\Server\Configuration
* @var \ActivityPhp\Server\Configuration|null
*/
protected $configuration;
private $configuration;

/**
* Server constructor
*
* @param array $config Server configuration
*
* @param array<string,mixed> $config Server configuration
*/
public function __construct(array $config = [])
{
Expand All @@ -72,28 +75,23 @@ public function __construct(array $config = [])

/**
* Get logger instance
*
* @return null|\Psr\Log\LoggerInterface
*/
public function logger()
public function logger(): ?LoggerInterface
{
return $this->logger;
}

/**
* Get cache instance
*
* @return null|\Psr\Cache\CacheItemPoolInterface
*/
public function cache()
public function cache(): ?CacheItemPoolInterface
{
return $this->cache;
}

/**
* Get a configuration handler
*
* @param string $parameter
*
* @return \ActivityPhp\Server\Configuration\LoggerConfiguration
* | \ActivityPhp\Server\Configuration\InstanceConfiguration
* | \ActivityPhp\Server\Configuration\HttpConfiguration
Expand All @@ -107,11 +105,10 @@ public function config(string $parameter)
/**
* Get an inbox instance
* It's a local instance
*
*
* @param string $handle An actor name
* @return \ActivityPhp\Server\Actor\Inbox
*/
public function inbox(string $handle)
public function inbox(string $handle): Inbox
{
$this->logger()->info($handle . ':' . __METHOD__);

Expand All @@ -130,11 +127,8 @@ public function inbox(string $handle)
/**
* Get an outbox instance
* It may be a local or a distant outbox.
*
* @param string $handle
* @return \ActivityPhp\Server\Actor\Outbox
*/
public function outbox(string $handle)
public function outbox(string $handle): Outbox
{
$this->logger()->info($handle . ':' . __METHOD__);

Expand All @@ -152,11 +146,8 @@ public function outbox(string $handle)

/**
* Build an server-oriented actor object
*
* @param string $handle
* @return \ActivityPhp\Server\Actor
*/
public function actor(string $handle)
public function actor(string $handle): Actor
{
$this->logger()->info($handle . ':' . __METHOD__);

Expand All @@ -171,6 +162,8 @@ public function actor(string $handle)

/**
* Get server instance with a static call
*
* @param array<string,string|int|array<string>> $settings
*/
public static function server(array $settings = []): self
{
Expand Down
2 changes: 1 addition & 1 deletion src/ActivityPhp/Server/Http/HttpSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class HttpSignature
{
const SIGNATURE_PATTERN = '/^
public const SIGNATURE_PATTERN = '/^
keyId="(?P<keyId>
(https?:\/\/[\w\-\.]+[\w]+)
(:[\d]+)?
Expand Down
53 changes: 25 additions & 28 deletions src/ActivityPhp/Type.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ActivityPhp package.
*
Expand All @@ -19,59 +21,56 @@

/**
* \ActivityPhp\Type is a Factory for ActivityStreams 2.0 types.
*
*
* It provides shortcuts methods for type instanciation and more.
*
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#types
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
*/
*/
abstract class Type
{
/**
* Factory method to create type instance and set attributes values
*
*
* To see which default types are defined and their attributes:
*
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#types
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
*
* @param string|array $type
* @param array $attributes
* @return \ActivityPhp\Type\AbstractObject
*
* @param string|array<string,mixed> $type
* @param array<string,mixed> $attributes
*/
public static function create($type, array $attributes = [])
public static function create($type, array $attributes = []): AbstractObject
{
if (!is_string($type) && !is_array($type)) {
if (! is_string($type) && ! is_array($type)) {
throw new Exception(
"Type parameter must be a string or an array. Given="
'Type parameter must be a string or an array. Given='
. gettype($type)
);
}

if (is_array($type)) {
if (!isset($type['type'])) {
if (! isset($type['type'])) {
throw new Exception(
"Type parameter must have a 'type' key"
);
} else {
$attributes = $type;
}

$attributes = $type;
}

try {

$class = is_array($type)
? TypeResolver::getClass($type['type'])
: TypeResolver::getClass($type);

} catch(Exception $e) {
} catch (Exception $exception) {
$message = json_encode($attributes, JSON_PRETTY_PRINT);
throw new Exception(
$e->getMessage() . "\n$message"
$exception->getMessage() . "\n{$message}"
);
}

Expand Down Expand Up @@ -112,23 +111,23 @@ public static function fromJson(string $json): AbstractObject
/**
* Add a custom type definition
* It overrides defined types
*
*
* @param string $name A short name.
* @param string $class Fully qualified class name
*/
public static function add($name, $class)
public static function add(string $name, string $class): void
{
TypeResolver::addCustomType($name, $class);
}

/**
* Add a custom validator for an attribute.
* It checks that it implements Validator\Interface
*
*
* @param string $name An attribute name to validate.
* @param string $class A validator class name
*/
public static function addValidator($name, $class)
public static function addValidator(string $name, string $class): void
{
Validator::add($name, $class);
}
Expand All @@ -138,15 +137,13 @@ public static function addValidator($name, $class)
* vocabulary.
* They extends basic protocol with custom properties.
* These extensions are called dialects.
*
*
* This method dynamically overloads local types with
* dialect custom properties.
*
* @param \ActivityPhp\Type\AbstractObject $type
*/
private static function extend(AbstractObject $type)
private static function extend(AbstractObject $type): void
{
// @todo should call Dialect stack to see if there are any
// @todo should call Dialect stack to see if there are any
// properties to overloads $type with
Dialect::extend($type);
}
Expand Down
Loading

0 comments on commit 7aca2db

Please sign in to comment.