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

Added Code Analyser #28

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ body:
label: "🐘 PHP Version"
description: "What version of PHP are you running?"
options:
- PHP 7.3.x
- PHP 7.4.x
- PHP 8.0
- Different version (specify in environment)

- type: dropdown
id: operating-system
attributes:
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "CodeQL"

on: [pull_request]
jobs:
lint:
name: CodeQL
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2

- run: git checkout HEAD^2

- name: Run CodeQL
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer check"
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ before_script: composer install --ignore-platform-reqs

script:
- vendor/bin/phpunit --configuration phpunit.xml
- vendor/bin/psalm --show-info=true
- vendor/bin/psalm --show-info=true
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"keywords": ["php","framework", "upf", "utopia", "analytics"],
"license": "MIT",
"minimum-stability": "stable",
"scripts": {
"check": "./vendor/bin/phpstan analyse --level 6 src tests"
},
"autoload": {
"psr-4": {"Utopia\\Analytics\\": "src/Analytics"}
},
Expand All @@ -14,6 +17,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"phpstan/phpstan": "1.9.x-dev",
"vimeo/psalm": "^5.6"
}
}
44 changes: 24 additions & 20 deletions src/Analytics/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class Adapter

/**
* The IP address to forward to Plausible
*
*
* @var string
*/
protected string $clientIP;
Expand All @@ -32,23 +32,23 @@ abstract class Adapter

/**
* Gets the name of the adapter.
*
*
* @return string
*/
abstract public function getName(): string;

/**
* Global Headers
*
* @var array
* @var array<int|string,mixed>
*/
protected $headers = [
'Content-Type' => '',
];

/**
* Enables tracking for this instance.
*
*
* @return void
*/
public function enable(): void
Expand All @@ -58,7 +58,7 @@ public function enable(): void

/**
* Disables tracking for this instance.
*
*
* @return void
*/
public function disable(): void
Expand All @@ -68,7 +68,7 @@ public function disable(): void

/**
* Send the event to the adapter.
*
*
* @param Event $event
* @return bool
*/
Expand All @@ -88,9 +88,11 @@ public abstract function validate(Event $event): bool;

/**
* Sets the client IP address.

*
* @param string $clientIP The IP address to use.
*

* @return self
*/
public function setClientIP(string $clientIP): self
Expand All @@ -101,9 +103,9 @@ public function setClientIP(string $clientIP): self

/**
* Sets the client user agent.
*
*
* @param string $userAgent The user agent to use.
*
*
* @return self
*/
public function setUserAgent(string $userAgent): self
Expand All @@ -114,7 +116,7 @@ public function setUserAgent(string $userAgent): self

/**
* Creates an Event on the remote analytics platform.
*
*
* @param Event $event
* @return bool
*/
Expand All @@ -135,12 +137,13 @@ public function createEvent(Event $event): bool
*
* @param string $method
* @param string $path
* @param array $params
* @param array $headers
* @return array|string
* @param array<int|string,mixed> $params
* @param array<int|string,mixed> $headers
*
* @return array<string, mixed>
* @throws \Exception
*/
public function call(string $method, string $path = '', array $headers = array(), array $params = array()): array|string
public function call(string $method, string $path = '', array $headers = array(), array $params = array()): array
{
$headers = array_merge($this->headers, $headers);
$ch = curl_init((str_contains($path, 'http') ? $path : $this->endpoint . $path . (($method == 'GET' && !empty($params)) ? '?' . http_build_query($params) : '')));
Expand Down Expand Up @@ -194,7 +197,7 @@ public function call(string $method, string $path = '', array $headers = array()

$responseType = $responseHeaders['Content-Type'] ?? '';
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);

switch(substr($responseType, 0, strpos($responseType, ';'))) {
case 'application/json':
$responseBody = json_decode($responseBody, true);
Expand All @@ -204,7 +207,7 @@ public function call(string $method, string $path = '', array $headers = array()
if (curl_errno($ch)) {
throw new \Exception(curl_error($ch));
}

curl_close($ch);

if($responseStatus >= 400) {
Expand All @@ -222,11 +225,12 @@ public function call(string $method, string $path = '', array $headers = array()
/**
* Flatten params array to PHP multiple format
*
* @param array $data
* @param array<int|string,mixed> $data
* @param string $prefix
* @return array
* @return array<string, mixed>
*/
protected function flatten(array $data, string $prefix = ''): array {
protected function flatten(array $data, string $prefix = ''): array
{
$output = [];

foreach($data as $key => $value) {
Expand All @@ -245,7 +249,7 @@ protected function flatten(array $data, string $prefix = ''): array {

/**
* Log Error
*
*
* @param Exception $e
* @return void
*/
Expand All @@ -256,4 +260,4 @@ protected function logError(Exception $e) {
Console::error('[Error] File: ' . $e->getFile());
Console::error('[Error] Line: ' . $e->getLine());
}
}
}
55 changes: 28 additions & 27 deletions src/Analytics/Adapter/ActiveCampaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ActiveCampaign extends Adapter

/**
* Gets the name of the adapter.
*
*
* @return string
*/
public function getName(): string
Expand All @@ -47,13 +47,14 @@ public function getName(): string

/**
* Checks if a contact exists by the email ID. Returns the User ID if it exists and false if it doesn't.
*
*
* @param string $email
* @return bool|int
*/
public function contactExists(string $email): bool|int
{
try {

$result = $this->call('GET', '/api/3/contacts', [], [
'email' => $email
]);
Expand All @@ -73,7 +74,7 @@ public function contactExists(string $email): bool|int

/**
* Create a contact
*
*
* @param string $email
* @param string $firstName
* @param string $lastName
Expand Down Expand Up @@ -102,13 +103,13 @@ public function createContact(string $email, string $firstName = '', string $las

/**
* Update contact
*
*
* @param string $contactId
* @param string $email
* @param string $firstName
* @param string $lastName
* @param string $phone
*
*
* @return bool
*/
public function updateContact(string $contactId, string $email, string $firstName = '', string $lastName = '', string $phone = ''): bool
Expand All @@ -132,9 +133,9 @@ public function updateContact(string $contactId, string $email, string $firstNam
}
}

/**
* Delete a contact
*
/**
* Delete a contact
*
* @param string $email
* @return bool
*/
Expand All @@ -157,7 +158,7 @@ public function deleteContact(string $email): bool

/**
* Account Exists
*
*
* @param string $name
* @return bool|int
*/
Expand All @@ -181,12 +182,12 @@ public function accountExists(string $name): bool|int

/**
* Create an account
*
*
* @param string $name
* @param string $url
* @param int $ownerId
* @param array $fields
*
* @param array<int|string,mixed> $fields
*
* @return bool
*/
public function createAccount(string $name, string $url = '', int $ownerId = 1, array $fields = []): bool
Expand All @@ -213,13 +214,13 @@ public function createAccount(string $name, string $url = '', int $ownerId = 1,

/**
* Update an account
*
*
* @param string $accountId
* @param string $name
* @param string $url
* @param int $ownerId
* @param array $fields
*
* @param array<int|string,mixed> $fields
*
* @return bool
*/
public function updateAccount(string $accountId, string $name, string $url = '', int $ownerId = 1, array $fields = []): bool
Expand All @@ -246,9 +247,9 @@ public function updateAccount(string $accountId, string $name, string $url = '',

/**
* Delete an account
*
*
* @param string $accountId
*
*
* @return bool
*/
public function deleteAccount(string $accountId): bool
Expand All @@ -264,13 +265,13 @@ public function deleteAccount(string $accountId): bool

/**
* Sync an association
*
*
* Creates an association if it doesn't exist and updates it if it does
*
*
* @param string $accountId
* @param string $contactId
* @param string $role
*
*
* @return bool
*/
public function syncAssociation(string $accountId, string $contactId, string $role = ''): bool
Expand Down Expand Up @@ -319,11 +320,11 @@ public function syncAssociation(string $accountId, string $contactId, string $ro
}

/**
* @param string $key
* @param string $key
* @param string $actId
* @param string $apiKey
* @param string $organisationId
*
*
* @return ActiveCampaign
*/
public function __construct(string $key, string $actId, string $apiKey, string $organisationId)
Expand All @@ -340,7 +341,7 @@ public function __construct(string $key, string $actId, string $apiKey, string $

/**
* Creates an Event on the remote analytics platform.
*
*
* @param Event $event
* @return bool
*/
Expand Down Expand Up @@ -370,9 +371,9 @@ public function send(Event $event): bool

/**
* Sets the client IP address.
*
*
* @param string $ip The IP address to use.
*
*
* @return self
*/
public function setClientIP(string $clientIP): self
Expand All @@ -382,9 +383,9 @@ public function setClientIP(string $clientIP): self

/**
* Sets the client user agent.
*
*
* @param string $userAgent The user agent to use.
*
*
* @return self
*/
public function setUserAgent(string $userAgent): self
Expand Down
Loading