Skip to content

Commit 64273eb

Browse files
REFACTOR - add parameter types and return types to class methods
1 parent 69551ba commit 64273eb

File tree

10 files changed

+186
-88
lines changed

10 files changed

+186
-88
lines changed

.vscode/launch.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Listen for XDebug",
9+
"type": "php",
10+
"request": "launch",
11+
"port": 9000
12+
},
13+
{
14+
"name": "Launch currently open script",
15+
"type": "php",
16+
"request": "launch",
17+
"program": "${file}",
18+
"cwd": "${fileDirname}",
19+
"port": 9000
20+
}
21+
]
22+
}

helpers/helper_functions.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
// Example usage: html_var_export($limit, '$limit');
4-
function html_var_export($var, $var_name = NULL)
4+
function html_var_export($var, bool $var_name = NULL): void
55
{
66
$output = '';
77

@@ -15,7 +15,7 @@ function html_var_export($var, $var_name = NULL)
1515
echo $output;
1616
}
1717

18-
function nl2br_and_nbsp($string)
18+
function nl2br_and_nbsp(string $string): string
1919
{
2020
$string = nl2br($string);
2121

@@ -24,7 +24,7 @@ function nl2br_and_nbsp($string)
2424
return $string;
2525
}
2626

27-
function nbsp($string)
27+
function nbsp(string $string): string
2828
{
2929
preg_replace('/\t/', '&nbsp;&nbsp;&nbsp;&nbsp;', $string);
3030

@@ -41,12 +41,12 @@ function nbsp($string)
4141
return $string;
4242
}
4343

44-
function print_var_name($var) {
44+
function print_var_name($var): ?string {
4545
foreach($GLOBALS as $var_name => $value) {
4646
if ($value === $var) {
4747
return $var_name;
4848
}
4949
}
5050

51-
return false;
51+
return NULL;
5252
}

index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
$start = $time;
77

88
error_reporting(E_ALL);
9-
ini_set('display_errors', 'On');
9+
ini_set('display_errors', 1);
1010

1111
require_once('helpers/helper_functions.php');
1212
require_once('models/ChessRulebook.php');

models/ChessBoard.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ChessBoard {
2929
public $halfmove_clock;
3030
public $fullmove_number;
3131

32-
function __construct($fen = self::DEFAULT_FEN) {
32+
function __construct(string $fen = self::DEFAULT_FEN) {
3333
$this->import_fen($fen);
3434
}
3535

@@ -47,7 +47,7 @@ function __clone() {
4747
}
4848
}
4949

50-
function import_fen($fen) {
50+
function import_fen(string $fen): void {
5151
// TODO: FEN probably needs its own class.
5252
// Then it can have a method for each section of code below.
5353

@@ -171,7 +171,7 @@ function import_fen($fen) {
171171
}
172172
}
173173

174-
function export_fen() {
174+
function export_fen(): string {
175175
$string = '';
176176

177177
// A chessboard looks like this
@@ -250,7 +250,7 @@ function export_fen() {
250250
}
251251

252252
// Keeping this for debug reasons.
253-
function get_ascii_board() {
253+
function get_ascii_board(): string {
254254
$string = '';
255255

256256
if ( $this->color_to_move == ChessPiece::WHITE ) {
@@ -284,7 +284,7 @@ function get_ascii_board() {
284284
return $string;
285285
}
286286

287-
function get_graphical_board() {
287+
function get_graphical_board(): array {
288288
// We need to throw some variables into an array so our view can build the board.
289289
// The array shall be in the following format:
290290
// square_color = black / white
@@ -325,7 +325,7 @@ function get_graphical_board() {
325325
return $graphical_board_array;
326326
}
327327

328-
function get_side_to_move_string() {
328+
function get_side_to_move_string(): string {
329329
$string = '';
330330

331331
if ( $this->color_to_move == ChessPiece::WHITE ) {
@@ -337,7 +337,7 @@ function get_side_to_move_string() {
337337
return $string;
338338
}
339339

340-
function get_who_is_winning_string() {
340+
function get_who_is_winning_string(): string {
341341
$points = 0;
342342

343343
foreach ( $this->board as $value1 ) {
@@ -358,21 +358,21 @@ function get_who_is_winning_string() {
358358
}
359359
}
360360

361-
function invert_rank_or_file_number($number) {
361+
function invert_rank_or_file_number(int $number): int {
362362
// 1 => 8
363363
// 2 => 7
364364
// etc.
365365

366366
return 9 - $number;
367367
}
368368

369-
function number_to_file($number) {
369+
function number_to_file(int $number) {
370370
return self::FILE_NUMS_AND_LETTERS[$number];
371371
}
372372

373373
// Note: This does not check for and reject illegal moves. It is up to code in the ChessGame class to generate a list of legal moves, then only make_move those moves.
374374
// In fact, sometimes make_move will be used on illegal moves (king in check moves), then the illegal moves will be deleted from the list of legal moves in a later step.
375-
function make_move($old_square, $new_square) {
375+
function make_move(ChessSquare $old_square, ChessSquare $new_square): void {
376376
$moving_piece = clone $this->board[$old_square->rank][$old_square->file];
377377

378378
$this->en_passant_target_square = NULL;
@@ -401,7 +401,7 @@ function make_move($old_square, $new_square) {
401401

402402
// Used to move the rook during castling.
403403
// Can't use make_move because it messes up color_to_move, halfmove, and fullmove.
404-
function make_additional_move_on_same_turn($old_square, $new_square) {
404+
function make_additional_move_on_same_turn(ChessSquare $old_square, ChessSquare $new_square): void {
405405
$moving_piece = clone $this->board[$old_square->rank][$old_square->file];
406406

407407
$this->board[$new_square->rank][$new_square->file] = $moving_piece;
@@ -412,15 +412,15 @@ function make_additional_move_on_same_turn($old_square, $new_square) {
412412
$this->board[$old_square->rank][$old_square->file] = NULL;
413413
}
414414

415-
function flip_color_to_move() {
415+
function flip_color_to_move(): void {
416416
if ( $this->color_to_move == ChessPiece::WHITE ) {
417417
$this->color_to_move = ChessPiece::BLACK;
418418
} elseif ( $this->color_to_move == ChessPiece::BLACK ) {
419419
$this->color_to_move = ChessPiece::WHITE;
420420
}
421421
}
422422

423-
function square_is_occupied($square) {
423+
function square_is_occupied(ChessSquare $square): bool {
424424
$rank = $square->rank;
425425
$file = $square->file;
426426

@@ -431,7 +431,7 @@ function square_is_occupied($square) {
431431
}
432432
}
433433

434-
function get_king_square($color) {
434+
function get_king_square($color): ?ChessSquare {
435435
foreach ( $this->board as $rank ) {
436436
foreach ( $rank as $piece ) {
437437
if ( $piece ) {
@@ -445,14 +445,14 @@ function get_king_square($color) {
445445
return NULL;
446446
}
447447

448-
function remove_piece_from_square($square) {
448+
function remove_piece_from_square(ChessSquare $square): void {
449449
$rank = $square->rank;
450450
$file = $square->file;
451451

452452
$this->board[$rank][$file] = NULL;
453453
}
454454

455-
function count_pieces_on_rank($type, $rank, $color) {
455+
function count_pieces_on_rank($type, int $rank, $color): int {
456456
$count = 0;
457457

458458
for ( $i = 1; $i <= 8; $i++ ) {
@@ -468,7 +468,7 @@ function count_pieces_on_rank($type, $rank, $color) {
468468
return $count;
469469
}
470470

471-
function count_pieces_on_file($type, $file, $color) {
471+
function count_pieces_on_file($type, int $file, $color): int {
472472
$count = 0;
473473

474474
for ( $i = 1; $i <= 8; $i++ ) {

models/ChessMove.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class ChessMove {
2525
public $board;
2626

2727
function __construct(
28-
$starting_square,
29-
$ending_square,
28+
ChessSquare $starting_square,
29+
ChessSquare $ending_square,
3030
$color,
3131
$piece_type,
32-
$capture,
33-
$old_board,
34-
$store_board = TRUE
32+
bool $capture,
33+
ChessBoard $old_board,
34+
bool $store_board = TRUE
3535
) {
3636
$this->starting_square = $starting_square;
3737
$this->ending_square = $ending_square;
@@ -55,7 +55,7 @@ function __construct(
5555
}
5656
}
5757

58-
function possibly_remove_our_castling_privileges() {
58+
function possibly_remove_our_castling_privileges(): void {
5959
// if our king or rook moves update the FEN to take away our castling privileges
6060
if ( $this->color == ChessPiece::BLACK ) {
6161
if ( $this->piece_type == ChessPiece::KING && $this->starting_square->get_alphanumeric() == 'e8' ) {
@@ -78,7 +78,7 @@ function possibly_remove_our_castling_privileges() {
7878
}
7979
}
8080

81-
function possibly_remove_enemy_castling_privileges() {
81+
function possibly_remove_enemy_castling_privileges(): void {
8282
// If an enemy rook is captured, update the FEN to take away enemy castling privileges.
8383
// We'll keep it simple. Anytime a piece moves into a corner square (a1, a8, h1, h8),
8484
// remove the other side's castling privileges.
@@ -97,7 +97,7 @@ function possibly_remove_enemy_castling_privileges() {
9797
}
9898
}
9999

100-
function if_castling_move_rook() {
100+
function if_castling_move_rook(): void {
101101

102102
// if castling, move the rook into the right place
103103
if ( $this->color == ChessPiece::BLACK ) {
@@ -152,7 +152,7 @@ function __clone() {
152152
}
153153
}
154154

155-
function set_promotion_piece($piece_type) {
155+
function set_promotion_piece($piece_type): void {
156156
// update the piece
157157
$rank = $this->ending_square->rank;
158158
$file = $this->ending_square->file;
@@ -164,7 +164,7 @@ function set_promotion_piece($piece_type) {
164164
$this->promotion_piece_type = $piece_type;
165165
}
166166

167-
function get_notation() {
167+
function get_notation(): string {
168168
$string = '';
169169

170170
if (
@@ -241,7 +241,7 @@ function get_notation() {
241241
return $string;
242242
}
243243

244-
function get_coordinate_notation() {
244+
function get_coordinate_notation(): string {
245245
// Automatically pick queen when drag and dropping.
246246
if (
247247
$this->promotion_piece_type == ChessPiece::ROOK ||
@@ -254,7 +254,7 @@ function get_coordinate_notation() {
254254
}
255255
}
256256

257-
function get_piece_letter() {
257+
function get_piece_letter(): string {
258258
return strtoupper(self::PIECE_LETTERS[$this->piece_type]);
259259
}
260260
}

models/ChessPiece.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ChessPiece
7878
self::BLACK => -1
7979
);
8080

81-
function __construct($color, $square_string, $type) {
81+
function __construct($color, string $square_string, $type) {
8282
if ( in_array($color, self::VALID_COLORS) ) {
8383
$this->color = $color;
8484
} else {
@@ -98,17 +98,17 @@ function __clone() {
9898
$this->square = clone $this->square;
9999
}
100100

101-
function get_unicode_symbol()
101+
function get_unicode_symbol(): string
102102
{
103103
return self::UNICODE_CHESS_PIECES[$this->color][$this->type];
104104
}
105105

106-
function get_fen_symbol()
106+
function get_fen_symbol(): string
107107
{
108108
return self::FEN_CHESS_PIECES[$this->color][$this->type];
109109
}
110110

111-
function on_rank($rank)
111+
function on_rank(int $rank): bool
112112
{
113113
if ( $rank == $this->square->rank ) {
114114
return TRUE;
@@ -117,7 +117,7 @@ function on_rank($rank)
117117
}
118118
}
119119

120-
function get_value() {
120+
function get_value(): int {
121121
return self::PIECE_VALUES[$this->type] * self::SIDE_VALUES[$this->color];
122122
}
123123
}

0 commit comments

Comments
 (0)