From ebf0b71cb81db506b018a1da12edea635f605937 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Wed, 14 Jun 2023 11:06:55 +0000
Subject: [PATCH] Add parameter/property type hints

---
 src/Synchronizer.php           |   16 ++++++--
 src/ContractorSynchronizer.php |    6 +-
 src/InvoiceSynchronizer.php    |   14 +++---
 src/wFirmaApiFactory.php       |    2 
 src/PaymentSynchronizer.php    |   12 +++---
 src/UcrmHelper.php             |   22 +++++-----
 src/UcrmAttributes.php         |   12 +++---
 src/UcrmPaymentMethods.php     |    6 +-
 8 files changed, 49 insertions(+), 41 deletions(-)

diff --git a/src/ContractorSynchronizer.php b/src/ContractorSynchronizer.php
index 08c076c..aac6359 100644
--- a/src/ContractorSynchronizer.php
+++ b/src/ContractorSynchronizer.php
@@ -2,11 +2,11 @@
 
 namespace SIPL\UCRM\wFirma;
 
-use \Webit\WFirmaSDK\Contractors as Contractors;
-use \Webit\WFirmaSDK\Payments as Payments;
+use Webit\WFirmaSDK\Contractors as Contractors;
+use Webit\WFirmaSDK\Payments as Payments;
 
 class ContractorSynchronizer extends Synchronizer {
-	function synchronize(int $ucrmClientId) {
+	function synchronize(int $ucrmClientId): bool {
 		$crm = $this->helper->getApi();
 		$wFirmaContractors = $this->wfirma->contractorsApi();
 
diff --git a/src/InvoiceSynchronizer.php b/src/InvoiceSynchronizer.php
index c16d17c..d51c4b0 100644
--- a/src/InvoiceSynchronizer.php
+++ b/src/InvoiceSynchronizer.php
@@ -2,12 +2,12 @@
 
 namespace SIPL\UCRM\wFirma;
 
-use \Webit\WFirmaSDK\Contractors as Contractors;
-use \Webit\WFirmaSDK\Invoices as Invoices;
-use \Webit\WFirmaSDK\Payments as Payments;
+use Webit\WFirmaSDK\Contractors as Contractors;
+use Webit\WFirmaSDK\Invoices as Invoices;
+use Webit\WFirmaSDK\Payments as Payments;
 
 class InvoiceSynchronizer extends Synchronizer {
-	function getContractorId($clientId, $synchronize = TRUE) {
+	protected function getContractorId($clientId, $synchronize = TRUE): ?Contractors\ContractorId {
 		if ($synchronize) {
 			$synchronizer = new ContractorSynchronizer($this->wfirma, $this->helper);
 			$synchronizer->synchronize($clientId);
@@ -29,7 +29,7 @@
 		return Contractors\ContractorId::create($wFirmaId);
 	}
 
-	function getTaxes() {
+	protected function getTaxes(): array {
 		$crm = $this->helper->getApi();
 		$taxesData = $crm->get('/taxes');
 
@@ -40,14 +40,14 @@
 		return $taxes;
 	}
 
-	function compareInvoicesContent(Invoices\InvoicesContent $c1, Invoices\InvoicesContent $c2) {
+	protected function compareInvoicesContent(Invoices\InvoicesContent $c1, Invoices\InvoicesContent $c2): int {
 		return
 			[$c1->name(), $c1->unit(), $c1->count(), $c1->price(), $c1->vat(), $c1->discount()]
 			<=>
 			[$c2->name(), $c2->unit(), $c2->count(), $c2->price(), $c2->vat(), $c2->discount()];
 	}
 
-	function synchronize(int $ucrmInvoiceId) {
+	function synchronize(int $ucrmInvoiceId): bool {
 		$crm = $this->helper->getApi();
 		$wFirmaInvoices = $this->wfirma->invoicesApi();
 
diff --git a/src/PaymentSynchronizer.php b/src/PaymentSynchronizer.php
index f6f190a..6037f55 100644
--- a/src/PaymentSynchronizer.php
+++ b/src/PaymentSynchronizer.php
@@ -2,8 +2,8 @@
 
 namespace SIPL\UCRM\wFirma;
 
-use \Webit\WFirmaSDK\Invoices as Invoices;
-use \Webit\WFirmaSDK\Payments as Payments;
+use Webit\WFirmaSDK\Invoices as Invoices;
+use Webit\WFirmaSDK\Payments as Payments;
 
 class PaymentSynchronizer extends Synchronizer {
 	protected $ucrmMainDir;
@@ -14,17 +14,17 @@
 		$backtrace = debug_backtrace();
 		$backtrace = end($backtrace);
 		// (...)/web/_plugins/wfirma/public.php
-		$this->ucrmMainDir = dirname(dirname(dirname(dirname($backtrace['file']))));
+		$this->ucrmMainDir = dirname($backtrace['file'], 4);
 	}
 
-	function comparePayment(Payments\Payment $p1, Payments\Payment $p2) {
+	function comparePayment(Payments\Payment $p1, Payments\Payment $p2): int {
 		return
 			[$p1->objectName(), $p1->objectId(), $p1->amount()->value(), $p1->date()->format('Y-m-d'), $p1->paymentMethod()]
 			<=>
 			[$p2->objectName(), $p2->objectId(), $p2->amount()->value(), $p2->date()->format('Y-m-d'), $p2->paymentMethod()];
 	}
 
-	function synchronize(int $ucrmPaymentId) {
+	function synchronize(int $ucrmPaymentId): bool {
 		$crm = $this->helper->getApi();
 		$wFirmaPaymentsApi = $this->wfirma->paymentsApi();
 
@@ -171,7 +171,7 @@
 		return $changed;
 	}
 
-	function delete(array $paymentData) {
+	function delete(array $paymentData): bool {
 		$wFirmaPaymentsApi = $this->wfirma->paymentsApi();
 		$paymentAttributeId = $this->helper->getAttributes()->getIdForCode('payment');
 
diff --git a/src/Synchronizer.php b/src/Synchronizer.php
index 7aeda66..58e04f1 100644
--- a/src/Synchronizer.php
+++ b/src/Synchronizer.php
@@ -3,19 +3,27 @@
 namespace SIPL\UCRM\wFirma;
 
 abstract class Synchronizer {
-	protected $wfirma;
-	protected $helper;
+	protected \Webit\WFirmaSDK\Entity\ModuleApiFactory $wfirma;
+	protected UcrmHelper $helper;
 
 	function __construct(\Webit\WFirmaSDK\Entity\ModuleApiFactory $wFirmaApi, UcrmHelper $ucrmHelper) {
 		$this->wfirma = $wFirmaApi;
 		$this->helper = $ucrmHelper;
 	}
 
-	public function synchronize(int $entityId) {
+	/**
+	 * @param int $entityId
+	 * @return bool TRUE if anything was modified, FALSE otherwise
+	 */
+	public function synchronize(int $entityId): bool {
 		return FALSE;
 	}
 
-	public function delete(array $entity) {
+	/**
+	 * @param array $entity
+	 * @return bool TRUE if anything was deleted, FALSE otherwise
+	 */
+	public function delete(array $entity): bool {
 		return FALSE;
 	}
 }
diff --git a/src/UcrmAttributes.php b/src/UcrmAttributes.php
index 5782706..dfe9d28 100644
--- a/src/UcrmAttributes.php
+++ b/src/UcrmAttributes.php
@@ -3,10 +3,10 @@
 namespace SIPL\UCRM\wFirma;
 
 class UcrmAttributes {
-	protected $attributesFile;
-	protected $api;
+	protected string $attributesFile;
+	protected \Ubnt\UcrmPluginSdk\Service\UcrmApi $api;
 
-	protected static $definition = [
+	protected static array $definition = [
 		'wFirma Customer ID' => [
 			'code' => 'client',
 			'type' => 'client',
@@ -20,7 +20,7 @@
 			'type' => 'payment',
 		],
 	];
-	protected $ids = [];
+	protected array $ids = [];
 
 	function __construct(UcrmHelper $ucrmHelper) {
 		$this->attributesFile = $ucrmHelper->getRootDirectory() . '/data/attributes.json';
@@ -41,7 +41,7 @@
 		return $this->ids[$attributeCode];
 	}
 
-	function updateMapping() {
+	function updateMapping(): bool {
 		$attributes = $this->api->get('/custom-attributes');
 		foreach ($attributes as $attribute) {
 			$data = self::$definition[$attribute['name']] ?? NULL;
@@ -71,7 +71,7 @@
 		return $changed;
 	}
 
-	function saveMapping() {
+	function saveMapping(): void {
 		if (!is_dir(dirname($this->attributesFile))) {
 			mkdir(dirname($this->attributesFile));
 		}
diff --git a/src/UcrmHelper.php b/src/UcrmHelper.php
index bc14978..830c923 100644
--- a/src/UcrmHelper.php
+++ b/src/UcrmHelper.php
@@ -3,10 +3,10 @@
 namespace SIPL\UCRM\wFirma;
 
 class UcrmHelper {
-	protected $rootDirectory;
-	protected $api = NULL;
-	protected $attributes = NULL;
-	protected $paymentMethods = NULL;
+	protected string $rootDirectory;
+	protected ?\Ubnt\UcrmPluginSdk\Service\UcrmApi $api = NULL;
+	protected ?UcrmAttributes $attributes = NULL;
+	protected ?UcrmPaymentMethods $paymentMethods = NULL;
 	protected $config = NULL;
 	protected $event = NULL;
 
@@ -17,32 +17,32 @@
 		$this->rootDirectory = $rootDirectory;
 	}
 
-	function getRootDirectory() {
+	function getRootDirectory(): string {
 		return $this->rootDirectory;
 	}
 
-	function getApi() {
+	function getApi(): \Ubnt\UcrmPluginSdk\Service\UcrmApi {
 		if ($this->api === NULL) {
 			$this->api = \Ubnt\UcrmPluginSdk\Service\UcrmApi::create($this->rootDirectory);
 		}
 		return $this->api;
 	}
 
-	function getAttributes() {
+	function getAttributes(): UcrmAttributes {
 		if ($this->attributes === NULL) {
 			$this->attributes = new UcrmAttributes($this);
 		}
 		return $this->attributes;
 	}
 
-	function getPaymentMethods() {
+	function getPaymentMethods(): UcrmPaymentMethods {
 		if ($this->paymentMethods === NULL) {
 			$this->paymentMethods = new UcrmPaymentMethods($this);
 		}
 		return $this->paymentMethods;
 	}
 
-	function getConfig() {
+	function getConfig(): array {
 		if ($this->config === NULL) {
 			$configManager = \Ubnt\UcrmPluginSdk\Service\PluginConfigManager::create($this->rootDirectory);
 			$this->config = $configManager->loadConfig();
@@ -50,12 +50,12 @@
 		return $this->config;
 	}
 
-	function getVersion() {
+	function getVersion(): string {
 		$response = $this->getApi()->get('/version');
 		return $response['version'];
 	}
 
-	function getCurrentEvent() {
+	function getCurrentEvent(): array {
 		if ($this->event === NULL) {
 			try {
 				if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
diff --git a/src/UcrmPaymentMethods.php b/src/UcrmPaymentMethods.php
index 052cfd3..577e516 100644
--- a/src/UcrmPaymentMethods.php
+++ b/src/UcrmPaymentMethods.php
@@ -3,8 +3,8 @@
 namespace SIPL\UCRM\wFirma;
 
 class UcrmPaymentMethods {
-	protected $api;
-	protected static $definition = [
+	protected \Ubnt\UcrmPluginSdk\Service\UcrmApi $api;
+	protected static array $definition = [
 		'Credit card' => '',
 		'Compensation' => '',
 	];
@@ -21,7 +21,7 @@
 		return self::$definition[$method];
 	}
 
-	function update() {
+	function update(): void {
 		$methods = $this->api->get('/payment-methods');
 		foreach ($methods as $method) {
 			if (isset(self::$definition[$method['name']])) {
diff --git a/src/wFirmaApiFactory.php b/src/wFirmaApiFactory.php
index 1ddcc70..a502603 100644
--- a/src/wFirmaApiFactory.php
+++ b/src/wFirmaApiFactory.php
@@ -3,7 +3,7 @@
 namespace SIPL\UCRM\wFirma;
 
 class wFirmaApiFactory {
-	protected $ucrmHelper;
+	protected UcrmHelper $ucrmHelper;
 
 	function __construct(UcrmHelper $ucrmHelper) {
 		$this->ucrmHelper = $ucrmHelper;

--
Gitblit v1.10.0