Skip to content

Commit

Permalink
add support for PHP8.1 and use Enums
Browse files Browse the repository at this point in the history
  • Loading branch information
noweh committed Nov 29, 2021
1 parent 0c4fdbd commit f0b62fa
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.4]
php: [8.1]
dependency-version: [prefer-stable]

steps:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"require": {
"ext-curl": "*",
"ext-json": "*",
"php": ">=7.4",
"php": ">=8.1",
"guzzlehttp/guzzle": "7.4.x-dev",
"guzzlehttp/oauth-subscriber": "0.6.x-dev"
},
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ parameters:
excludePaths:
- vendor

ignoreErrors:
# PHPStan does not supports Enums yet (v1.2.0)
- '/^Access to undefined constant Noweh\\TwitterApi\\Enum\\Operators::\w+\.$/'
- '/^Access to an undefined property Noweh\\TwitterApi\\Enum\\Operators::\$value\.$/'
- '/^Access to undefined constant Noweh\\TwitterApi\\Enum\\Modes::\w+\.$/'

# Inclusions
paths:
- .
14 changes: 7 additions & 7 deletions src/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@ abstract class AbstractController
/**
* @var string
*/
private $access_token;
private string $access_token;

/**
* @var string
*/
private $access_token_secret;
private string $access_token_secret;

/**
* @var string
*/
private $consumer_key;
private string $consumer_key;

/**
* @var string
*/
private $consumer_secret;
private string $consumer_secret;

/**
* @var string
*/
private $bearer_token;
private string $bearer_token;

/**
* @var string $endpoint
*/
private $endpoint;
private string $endpoint;

/**
* Creates object. Requires an array of settings.
Expand Down Expand Up @@ -81,7 +81,7 @@ public function __construct(array $settings = [])
* @throws \JsonException
* @throws Exception
*/
public function performRequest(string $method = 'GET', array $postData = [])
public function performRequest(string $method = 'GET', array $postData = []): mixed
{
try {
$headers = [
Expand Down
18 changes: 4 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ class Client
*/
protected array $settings = [];

public const OPERATORS = [
'OR' => 'OR',
'AND' => ''
];

public const MODES = [
'ID' => 'id',
'USERNAME' => 'username'
];

/**
* Client initialization
* @param array<string> $settings
Expand All @@ -35,7 +25,7 @@ public function __construct(array $settings)
* @return Retweet
* @throws \Exception
*/
public function retweet()
public function retweet(): Retweet
{
return new Retweet($this->settings);
}
Expand All @@ -45,7 +35,7 @@ public function retweet()
* @return Tweet
* @throws \Exception
*/
public function tweet()
public function tweet(): Tweet
{
return new Tweet($this->settings);
}
Expand All @@ -55,7 +45,7 @@ public function tweet()
* @return TweetSearch
* @throws \Exception
*/
public function tweetSearch()
public function tweetSearch(): TweetSearch
{
return new TweetSearch($this->settings);
}
Expand All @@ -65,7 +55,7 @@ public function tweetSearch()
* @return UserSearch
* @throws \Exception
*/
public function userSearch()
public function userSearch(): UserSearch
{
return new UserSearch($this->settings);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Enum/Modes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Noweh\TwitterApi\Enum;

enum Modes
{
case id;
case username;
}
9 changes: 9 additions & 0 deletions src/Enum/Operators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Noweh\TwitterApi\Enum;

enum Operators: string
{
case or = 'OR';
case and = '';
}
51 changes: 22 additions & 29 deletions src/TweetSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@

namespace Noweh\TwitterApi;

use Noweh\TwitterApi\Enum\Operators;

class TweetSearch extends AbstractController
{
public const OPERATORS = [
'OR' => 'OR',
'AND' => ''
];

/** @var array<string> $filteredUsernamesFrom */
private array $filteredUsernamesFrom = [];

/** @var string $operatorOnFilteredUsernamesFrom */
private string $operatorOnFilteredUsernamesFrom = self::OPERATORS['OR'];
/** @var Operators $operatorOnFilteredUsernamesFrom */
private Operators $operatorOnFilteredUsernamesFrom;

/** @var array<string> $filteredUsernamesTo */
private array $filteredUsernamesTo = [];

/** @var string $operatorOnFilteredUsernamesTo */
private string $operatorOnFilteredUsernamesTo = self::OPERATORS['OR'];
/** @var Operators $operatorOnFilteredUsernamesTo */
private Operators $operatorOnFilteredUsernamesTo;

/** @var array<string> $filteredKeywords */
private array $filteredKeywords = [];

/** @var string $operatorOnFilteredKeywords */
private string $operatorOnFilteredKeywords = self::OPERATORS['OR'];
/** @var Operators $operatorOnFilteredKeywords */
private Operators $operatorOnFilteredKeywords;

/** @var array<string> $filteredLocales */
private array $filteredLocales = [];
Expand Down Expand Up @@ -56,15 +53,13 @@ public function __construct(array $settings)
* Matches any Tweet from a specific user.
* The value can be either the username (excluding the @ character) or the user’s numeric user ID.
* @param array<string> $usernames
* @param string|null $operator
* @param Operators|null $operator
* @return TweetSearch
*/
public function addFilterOnUsernamesFrom(array $usernames, string $operator = null): TweetSearch
public function addFilterOnUsernamesFrom(array $usernames, Operators $operator = null): TweetSearch
{
$this->filteredUsernamesFrom = $usernames;
if (in_array($operator, self::OPERATORS, true)) {
$this->operatorOnFilteredUsernamesFrom = $operator;
}
$this->operatorOnFilteredUsernamesFrom = $operator instanceof Operators ? $operator : Operators::or;

return $this;
}
Expand All @@ -73,31 +68,27 @@ public function addFilterOnUsernamesFrom(array $usernames, string $operator = nu
* Matches any Tweet that is in reply to a particular user.
* The value can be either the username (excluding the @ character) or the user’s numeric user ID.
* @param array<string> $usernames
* @param string|null $operator
* @param Operators|null $operator
* @return TweetSearch
*/
public function addFilterOnUsernamesTo(array $usernames, string $operator = null): TweetSearch
public function addFilterOnUsernamesTo(array $usernames, Operators $operator = null): TweetSearch
{
$this->filteredUsernamesTo = $usernames;
if (in_array($operator, self::OPERATORS, true)) {
$this->operatorOnFilteredUsernamesTo = $operator;
}
$this->operatorOnFilteredUsernamesTo = $operator instanceof Operators ? $operator : Operators::or;

return $this;
}

/**
* Matches the exact phrase or a hashtag within the body of a Tweet.
* @param array<string> $keywords
* @param string|null $operator
* @param Operators|null $operator
* @return TweetSearch
*/
public function addFilterOnKeywordOrPhrase(array $keywords, string $operator = null): TweetSearch
public function addFilterOnKeywordOrPhrase(array $keywords, Operators $operator = null): TweetSearch
{
$this->filteredKeywords = $keywords;
if (in_array($operator, self::OPERATORS, true)) {
$this->operatorOnFilteredKeywords = $operator;
}
$this->operatorOnFilteredKeywords = $operator instanceof Operators ? $operator : Operators::or;

return $this;
}
Expand Down Expand Up @@ -192,20 +183,22 @@ protected function constructEndpoint(): string

$endpoint .= '("' . $keyword . '"%20OR%20%23' . $keyword . ')';
if ($qtyKeywords > 1 && $loop < $qtyKeywords) {
$endpoint .= '%20' . $this->operatorOnFilteredKeywords . '%20';
$endpoint .= '%20' . $this->operatorOnFilteredKeywords->value . '%20';
}
}
$endpoint .= ')';
}

if (!empty($this->filteredUsernamesFrom)) {
$endpoint .= '%20(from:' .
implode('%20' . $this->operatorOnFilteredUsernamesFrom . '%20from:', $this->filteredUsernamesFrom) . ')';
implode('%20' . $this->operatorOnFilteredUsernamesFrom->value . '%20from:', $this->filteredUsernamesFrom) .
')';
}

if (!empty($this->filteredUsernamesTo)) {
$endpoint .= '%20(to:' .
implode('%20' . $this->operatorOnFilteredUsernamesTo . '%20to:', $this->filteredUsernamesTo) . ')';
implode('%20' . $this->operatorOnFilteredUsernamesTo->value . '%20to:', $this->filteredUsernamesTo) .
')';
}

if (!empty($this->filteredLocales)) {
Expand Down
23 changes: 10 additions & 13 deletions src/UserSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

namespace Noweh\TwitterApi;

use Noweh\TwitterApi\Enum\Modes;

class UserSearch extends AbstractController
{
public const MODES = [
'ID' => 'id',
'USERNAME' => 'username'
];

/** @var mixed $idOrUsername */
private $idOrUsername;
private mixed $idOrUsername;

/** @var string $mode */
private string $mode = self::MODES['USERNAME'];
/** @var Modes $mode */
private Modes $mode = Modes::username;

/**
* @param array<int, string> $settings
Expand All @@ -28,13 +25,13 @@ public function __construct(array $settings)
/**
* returns details about up to 100 users by ID or Username
* @param mixed $idOrUsername can be an array of items
* @param string $mode
* @param Modes $mode
* @return UserSearch
*/
public function findByIdOrUsername($idOrUsername, string $mode = self::MODES['ID']): UserSearch
public function findByIdOrUsername(mixed $idOrUsername, Modes $mode = Modes::id): UserSearch
{
$this->idOrUsername = $idOrUsername;
if (in_array($mode, self::MODES, true)) {
if ($mode instanceof Modes) {
$this->mode = $mode;
}

Expand All @@ -59,14 +56,14 @@ protected function constructEndpoint(): string

$endpoint = parent::constructEndpoint();
if (is_array($this->idOrUsername)) {
if ($this->mode === self::MODES['USERNAME']) {
if ($this->mode === Modes::username) {
$endpoint .= '/by?usernames=';
} else {
$endpoint .= '?ids=';
}
$endpoint .= implode(',', $this->idOrUsername);
} else {
if ($this->mode === self::MODES['USERNAME']) {
if ($this->mode === Modes::username) {
$endpoint .= '/by/username';
}
$endpoint .= '/' . $this->idOrUsername;
Expand Down
7 changes: 4 additions & 3 deletions test/TwitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\TestCase;
use Dotenv\Dotenv;
use Noweh\TwitterApi\Client;
use Noweh\TwitterApi\Enum\Modes;

class TwitterTest extends TestCase
{
Expand All @@ -21,7 +22,7 @@ public function setUp(): void

$settings = [];
foreach (getenv() as $settingKey => $settingValue) {
if (strpos($settingKey, 'TWITTER_') === 0) {
if (str_starts_with($settingKey, 'TWITTER_')) {
$settings[str_replace('twitter_', '', mb_strtolower($settingKey))] = $settingValue;
}
}
Expand All @@ -48,7 +49,7 @@ public function testSearchUsers(): void
{
$this->assertIsObject(
$this->twitterClient->userSearch()
->findByIdOrUsername('twitterdev', Client::MODES['USERNAME'])
->findByIdOrUsername('twitterdev', Modes::username)
->performRequest()
);
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public function testRetweet(): void
* @return mixed
* @throws \JsonException|\Exception|\GuzzleHttp\Exception\GuzzleException
*/
private function searchWithParameters(array $keywords = [], array $usernames = [], $onlyWithMedia = false)
private function searchWithParameters(array $keywords = [], array $usernames = [], $onlyWithMedia = false): mixed
{
$request = $this->twitterClient->tweetSearch()
->showMetrics()
Expand Down

0 comments on commit f0b62fa

Please sign in to comment.