Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add larastan #60

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"pestphp/pest": "^1.21|^2.0",
"pestphp/pest-plugin-laravel": "^1.1",
"friendsofphp/php-cs-fixer": "^3.9",
"pestphp/pest-plugin-mock": "^1.0"
"larastan/larastan": "^2.0",
"mockery/mockery": "^1.6"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 18 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
includes:
- vendor/larastan/larastan/extension.neon

parameters:

paths:
- src/

# Level 9 is the highest level
level: 5

# ignoreErrors:
# - '#PHPDoc tag @var#'
#
# excludePaths:
# - ./*/*/FileToBeExcluded.php
#
# checkMissingIterableValueType: false
2 changes: 1 addition & 1 deletion src/Console/Commands/XeroKeepAliveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class XeroKeepAliveCommand extends Command
protected $signature = 'xero:keep-alive';
protected $description = 'Run this command to refresh token if its due to expire. schedule this to run daily to avoid token expiring when using CLI commands';

public function handle()
public function handle(): void
{
$this->newLine();
// Fetch all tenants for when multiple tenants are in use.
Expand Down
3 changes: 2 additions & 1 deletion src/Console/Commands/XeroShowAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ public function handle()
// Fetch all access tokens
$tokens = XeroToken::select($dataToDisplay)->get();

if(config('xero.encrypt')) {
if (config('xero.encrypt')) {
$tokens->map(function($token) {
try {
$access_token = Crypt::decryptString($token->access_token);
} catch (DecryptException $e) {
$access_token = $token->access_token;
$refresh_token = $token->refresh_token;
}

// Split them as a refresh token may not exist...
try {
$refresh_token = Crypt::decryptString($token->refresh_token);
Expand Down
14 changes: 13 additions & 1 deletion src/Models/XeroToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@
use DateTimeInterface;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
* @property int $id
* @property string $tenant_id
* @property string $tenant_name
* @property string $access_token
* @property string $refresh_token
* @property int $expires_in
* @property Carbon $created_at
* @property Carbon $updated_at
* @property mixed $expires
*/
class XeroToken extends Model
{
protected $guarded = [];

/**
* @return \Illuminate\Database\Eloquent\Casts\Attribute<DateTimeInterface, never>
* @return Attribute<Carbon, never>
*/
protected function expires(): Attribute
{
Expand Down
12 changes: 6 additions & 6 deletions src/Resources/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@

class Contacts extends Xero
{
public function get(int $page = null, string $where = null)
public function get(int $page = 1, string $where = ''): array
{
$params = http_build_query([
'page' => $page,
'where' => $where
]);

$result = $this->get('contacts?'.$params);
$result = parent::get('contacts?'.$params);

return $result['body']['Contacts'];
}

public function find(string $contactId)
public function find(string $contactId): array
{
$result = $this->get('contacts/'.$contactId);
$result = parent::get('contacts/'.$contactId);

return $result['body']['Contacts'][0];
}

public function update(string $contactId, array $data)
public function update(string $contactId, array $data): array
{
$result = $this->post('contacts/'.$contactId, $data);

return $result['body']['Contacts'][0];
}

public function store(array $data)
public function store(array $data): array
{
$result = $this->post('contacts', $data);

Expand Down
30 changes: 15 additions & 15 deletions src/Resources/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,60 @@

class Invoices extends Xero
{
public function get(int $page = null, string $where = null)
public function get(int $page = 1, string $where = ''): array
{
$params = http_build_query([
'page' => $page,
'where' => $where
]);

$result = $this->get('invoices?'.$params);
$result = parent::get('invoices?'.$params);

return $result['body']['Invoices'];
}

public function find(string $contactId)
public function find(string $contactId): array
{
$result = $this->get('invoices/'.$contactId);
$result = parent::get('invoices/'.$contactId);

return $result['body']['Invoices'][0];
}

public function onlineUrl(string $invoiceId)
public function onlineUrl(string $invoiceId): string
{
$result = $this->get('invoices/'.$invoiceId.'/OnlineInvoice');
$result = parent::get('invoices/'.$invoiceId.'/OnlineInvoice');

return $result['body']['OnlineInvoices'][0]['OnlineInvoiceUrl'];
}

public function update(string $invoiceId, array $data)
public function update(string $invoiceId, array $data): array
{
$result = $this->post('invoices/'.$invoiceId, $data);
$result = parent::post('invoices/'.$invoiceId, $data);

return $result['body']['Invoices'][0];
}

public function store(array $data)
public function store(array $data): array
{
$result = $this->post('invoices', $data);
$result = parent::post('invoices', $data);

return $result['body']['Invoices'][0];
}

public function attachments(string $invoiceId)
public function attachments(string $invoiceId): array
{
$result = Xero::get('invoices/'.$invoiceId.'/Attachments');
$result = parent::get('invoices/'.$invoiceId.'/Attachments');

return $result['body']['Attachments'];
}

public function attachment(string $invoiceId, string $attachmentId = null, string $fileName = null)
public function attachment(string $invoiceId, string $attachmentId = null, string $fileName = null): string
{
// Depending on the application we may want to get it by the FileName instead fo the AttachmentId
$nameOrId = $attachmentId ? $attachmentId : $fileName;

$result = Xero::get('invoices/'.$invoiceId.'/Attachments/'.$nameOrId, null, true);
$result = parent::get('invoices/'.$invoiceId.'/Attachments/'.$nameOrId);

return $result['body'];
}
}
}
9 changes: 4 additions & 5 deletions src/Resources/Webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@

class Webhooks extends Xero
{
protected $payload;
protected string $payload;

public function validate()
public function validate(): bool
{
$this->payload = file_get_contents("php://input");
$signature = $_SERVER['HTTP_X_XERO_SIGNATURE'];

return hash_equals($this->getSignature(), $signature);
}

public function getSignature()
public function getSignature(): string
{
return base64_encode(hash_hmac('sha256', $this->payload, config('xero.webhookKey'), true));
}

public function getEvents()
public function getEvents(): array
{
$this->validate();

$payload = json_decode($this->payload);

return $payload->events;
}

}
Loading