Skip to content

Commit 9f74673

Browse files
committed
Fix, by hand, hundreds of PHPStan errors mostly by declaring types.
There was some null checks added, and some other small tweaks here or there. Tools like rector dont seem to be able to help, so had to fix these by hand. Still 500+ errors (of interest) to clear down. The errors i'm considering "not interesting" are instances of using "array" when I should have been declaring a "class" (Object, Interface, ETC).
1 parent 74d067c commit 9f74673

File tree

402 files changed

+1440
-1309
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

402 files changed

+1440
-1309
lines changed

src/classes/App/ExceptionHandler.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77

88
class ExceptionHandler
99
{
10-
private $getDetails;
11-
private $changeStatus;
12-
private $stringTools;
10+
private GetDetails $getDetails;
11+
private ChangeStatus $changeStatus;
12+
private StringTools $stringTools;
1313

1414
/**
1515
* @Inject
1616
*/
17-
public function inject(GetDetails $getDetails, ChangeStatus $changeStatus, StringTools $stringTools)
17+
public function inject(GetDetails $getDetails, ChangeStatus $changeStatus, StringTools $stringTools) :void
1818
{
1919
$this->getDetails = $getDetails;
2020
$this->changeStatus = $changeStatus;
2121
$this->stringTools = $stringTools;
2222
}
2323

24-
public function register()
24+
public function register() :void
2525
{
2626
set_exception_handler([$this, "handle"]);
2727
}
2828

29-
public function handle($exception)
29+
public function handle($exception) :void
3030
{
3131
$message = $exception->getMessage();
3232

@@ -39,7 +39,7 @@ public function handle($exception)
3939
$hostId = $this->getDetails->getIdByUrlMatch($url);
4040
if (is_numeric($hostId)) {
4141
http_response_code(205);
42-
$this->changeStatus->setOffline($hostId);
42+
$this->changeStatus->setOffline((int) $hostId);
4343
}
4444
}
4545

src/classes/App/RouteApi.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212

1313
class RouteApi
1414
{
15-
private $container;
16-
private $recordAction;
17-
private $getDetails;
18-
private $hostList;
19-
private $fetchAllowedProjects;
20-
private $fetchUserDetails;
21-
private $fetchUserProject;
22-
private $invalidateToken;
15+
private Container $container;
16+
private RecordAction $recordAction;
17+
private GetDetails $getDetails;
18+
private HostList $hostList;
19+
private FetchAllowedProjects $fetchAllowedProjects;
20+
private FetchUserDetails $fetchUserDetails;
21+
private FetchUserProject $fetchUserProject;
22+
private InvalidateToken $invalidateToken;
2323

24-
private $project;
25-
private $userId;
24+
private ?string $project;
25+
private int $userId;
2626

2727

2828
public function __construct(
@@ -50,12 +50,12 @@ public function getRequestedProject()
5050
return $this->project;
5151
}
5252

53-
public function getUserId()
53+
public function getUserId() :int
5454
{
5555
return $this->userId;
5656
}
5757

58-
public function route($pathParts, $headers, $returnResult = false)
58+
public function route(array $pathParts, array $headers, bool $returnResult = false)
5959
{
6060
$userId = $headers["userid"];
6161

@@ -75,7 +75,7 @@ public function route($pathParts, $headers, $returnResult = false)
7575

7676
unset($pathParts[$methodkey]);
7777

78-
$controllerStr = "dhope0000\\LXDClient\\Controllers\\" . implode($pathParts, "\\");
78+
$controllerStr = "dhope0000\\LXDClient\\Controllers\\" . implode("\\", $pathParts);
7979
if (!class_exists($controllerStr)) {
8080
throw new \Exception("End point not found", 1);
8181
} elseif (method_exists($controllerStr, $method) !== true) {
@@ -90,8 +90,14 @@ public function route($pathParts, $headers, $returnResult = false)
9090
$this->recordAction->record($userId, $controllerStr . "\\" . $method, $params);
9191
}
9292

93+
$callback = array($controller, $method);
94+
95+
if (!is_callable($callback)) {
96+
throw new \Exception("Cant find route", 1);
97+
}
98+
9399
// TODO Pass provided arguments to controller
94-
$data = call_user_func_array(array($controller, $method), $params);
100+
$data = call_user_func_array($callback, $params);
95101

96102
if ($returnResult) {
97103
return $data;
@@ -101,7 +107,7 @@ public function route($pathParts, $headers, $returnResult = false)
101107
echo json_encode($data);
102108
}
103109

104-
public function orderParams($passedArguments, $class, $method, int $userId, $headers)
110+
public function orderParams(array $passedArguments, string $class, string $method, int $userId, array $headers) :array
105111
{
106112
$reflectedMethod = new \ReflectionMethod($class, $method);
107113
$paramaters = $reflectedMethod->getParameters();
@@ -124,7 +130,7 @@ public function orderParams($passedArguments, $class, $method, int $userId, $hea
124130
$hasDefault = $param->isDefaultValueAvailable();
125131

126132
$type = $param->getType();
127-
if (!empty($type)) {
133+
if (!empty($type) && $type instanceof \ReflectionNamedType) {
128134
$type = $type->getName();
129135
}
130136

@@ -186,7 +192,7 @@ public function orderParams($passedArguments, $class, $method, int $userId, $hea
186192
return $o;
187193
}
188194

189-
private function canAccessProject($allowedProjects, $hostId, $project)
195+
private function canAccessProject(array $allowedProjects, int $hostId, string $project) :void
190196
{
191197
if (!is_string($project) || strlen($project) == 0) {
192198
throw new \Exception("Cant work out which project to use", 1);

src/classes/App/RouteAssets.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class RouteAssets
55
{
6-
private $extensionMapping = [
6+
private array $extensionMapping = [
77
"css"=>"text/css",
88
"js"=>"text/javascript",
99
"png"=>"image/png",
@@ -12,20 +12,29 @@ class RouteAssets
1212
"woff2"=>"font/woff2"
1313
];
1414

15-
public function route($path)
15+
public function route(array $path) :void
1616
{
1717
$this->outputFile($path);
1818
}
1919

20-
public function outputFile($path)
20+
public function outputFile(array $path) :void
2121
{
22-
$path = __DIR__ . "/../../" . implode($path, "/");
22+
$path = __DIR__ . "/../../" . implode("/", $path);
2323
if (strpos($path, "?") !== false) {
2424
$path = substr($path, 0, strpos($path, "?"));
2525
}
2626

27+
if (!is_file($path)) {
28+
throw new \Exception("Cant find asset", 1);
29+
}
30+
2731
//get the last-modified-date of this very file
2832
$lastModified=filemtime($path);
33+
34+
if (!$lastModified) {
35+
throw new \Exception("Cant find asset", 1);
36+
}
37+
2938
//get a unique hash of this file (etag)
3039
$etagFile = md5_file($path);
3140
//get the HTTP_IF_MODIFIED_SINCE header if set

src/classes/App/RouteController.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414

1515
class RouteController
1616
{
17-
private $validateToken;
18-
private $userSession;
19-
private $logUserIn;
20-
private $routeApi;
21-
private $routeView;
22-
private $routeAssets;
23-
private $session;
24-
private $fetchUserDetails;
17+
private ValidateToken $validateToken;
18+
private UserSession $userSession;
19+
private LogUserIn $logUserIn;
20+
private RouteApi $routeApi;
21+
private RouteView $routeView;
22+
private RouteAssets $routeAssets;
23+
private Session $session;
24+
private FetchUserDetails $fetchUserDetails;
2525

26-
public $loginError = null;
26+
public ?string $loginError = null;
2727

2828
public function __construct(
2929
UserSession $userSession,
@@ -118,6 +118,11 @@ public function routeRequest($explodedPath)
118118
$path = "";
119119
if (isset($explodedPath[0])) {
120120
$parts = parse_url($explodedPath[0]);
121+
122+
if ($parts == false) {
123+
throw new \Exception("Couldn't parse '{$explodedPath[0]}'", 1);
124+
}
125+
121126
$path = $parts["path"];
122127
}
123128

src/classes/App/RouteView.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55

66
class RouteView
77
{
8-
private $container;
9-
8+
/** @phpstan-ignore-next-line */
9+
private Container $container;
10+
1011
public function __construct(Container $container)
1112
{
1213
$this->container = $container;
1314
}
1415

15-
public function route($pathParts)
16+
public function route(array $pathParts)
1617
{
1718
if (empty($pathParts)) {
1819
require __DIR__ . "/../../views/index.php";

src/classes/Controllers/AnalyticData/DownloadHistoryController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
class DownloadHistoryController
88
{
9-
private $downloadHistory;
10-
9+
private DownloadHistory $downloadHistory;
10+
1111
public function __construct(DownloadHistory $downloadHistory)
1212
{
1313
$this->downloadHistory = $downloadHistory;
1414
}
1515

16-
public function download(int $userId)
16+
public function download(int $userId) :array
1717
{
1818
return $this->downloadHistory->download($userId);
1919
}

src/classes/Controllers/Backups/GetBackupsOverviewController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
class GetBackupsOverviewController
88
{
9-
private $getBackupsOverview;
9+
private GetBackupsOverview $getBackupsOverview;
1010

1111
public function __construct(GetBackupsOverview $getBackupsOverview)
1212
{
1313
$this->getBackupsOverview = $getBackupsOverview;
1414
}
1515

16-
public function get($userId)
16+
public function get(int $userId) :array
1717
{
1818
return $this->getBackupsOverview->get($userId);
1919
}

src/classes/Controllers/Backups/RestoreBackupController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class RestoreBackupController implements \dhope0000\LXDClient\Interfaces\RecordAction
1010
{
11-
private $restoreBackup;
11+
private RestoreBackup $restoreBackup;
1212

1313
public function __construct(RestoreBackup $restoreBackup)
1414
{
@@ -17,7 +17,7 @@ public function __construct(RestoreBackup $restoreBackup)
1717
/**
1818
* @Route("", name="Restore Local Backup To Host")
1919
*/
20-
public function restore(int $userId, int $backupId, Host $targetHost)
20+
public function restore(int $userId, int $backupId, Host $targetHost) :array
2121
{
2222
$response = $this->restoreBackup->restore($userId, $backupId, $targetHost);
2323
return ["state"=>"success", "message"=>"Restored Backup", "lxdResponse"=>$response];

src/classes/Controllers/Backups/Strategies/GetStrategiesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
class GetStrategiesController
88
{
9-
private $fetchStrategies;
9+
private FetchStrategies $fetchStrategies;
1010

1111
public function __construct(FetchStrategies $fetchStrategies)
1212
{
1313
$this->fetchStrategies = $fetchStrategies;
1414
}
1515

16-
public function get()
16+
public function get() :array
1717
{
1818
return $this->fetchStrategies->fetchAll();
1919
}

src/classes/Controllers/CloudConfig/CreateController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class CreateController implements \dhope0000\LXDClient\Interfaces\RecordAction
88
{
9-
private $create;
9+
private Create $create;
1010

1111
public function __construct(Create $create)
1212
{
@@ -15,7 +15,7 @@ public function __construct(Create $create)
1515
/**
1616
* @Route("", name="Create Cloud Config")
1717
*/
18-
public function create(string $name, string $namespace, $description = "")
18+
public function create(string $name, string $namespace, string $description = "")
1919
{
2020
$this->create->create($name, $namespace, $description);
2121
return ["state"=>"success", "message"=>"Created cloud config"];

0 commit comments

Comments
 (0)