1 | <?php |
||||
2 | |||||
3 | require "vendor/autoload.php"; |
||||
4 | use MidoriKocak\GameOfLife\GameOfLife; |
||||
5 | |||||
6 | $climate = new League\CLImate\CLImate; |
||||
7 | |||||
8 | $climate->arguments->add([ |
||||
9 | 'random' => [ |
||||
10 | 'prefix' => 'r', |
||||
11 | 'longPrefix' => 'random', |
||||
12 | 'description' => 'Create Game of life from random matrix', |
||||
13 | 'noValue' => true, |
||||
14 | ], |
||||
15 | 'iterations' => [ |
||||
16 | 'prefix' => 'i', |
||||
17 | 'longPrefix' => 'iterations', |
||||
18 | 'description' => 'Number of iterations', |
||||
19 | 'castTo' => 'int', |
||||
20 | ], |
||||
21 | 'size' => [ |
||||
22 | 'prefix' => 's', |
||||
23 | 'longPrefix' => 'size', |
||||
24 | 'description' => 'Square size of the world', |
||||
25 | 'castTo' => 'int', |
||||
26 | ], |
||||
27 | 'species' => [ |
||||
28 | 'prefix' => 'sp', |
||||
29 | 'longPrefix' => 'species', |
||||
30 | 'description' => 'Amount of species', |
||||
31 | 'castTo' => 'int', |
||||
32 | ], |
||||
33 | 'filename' => [ |
||||
34 | 'prefix' => 'f', |
||||
35 | 'longPrefix' => 'filename', |
||||
36 | 'description' => 'File Name of XML input', |
||||
37 | 'castTo' => 'string', |
||||
38 | ], |
||||
39 | 'outputFilename' => [ |
||||
40 | 'prefix' => 'o', |
||||
41 | 'longPrefix' => 'outputFilename', |
||||
42 | 'description' => 'File Name of XML output', |
||||
43 | 'castTo' => 'string', |
||||
44 | 'defaultValue' => 'out.xml', |
||||
45 | ], |
||||
46 | 'verbose' => [ |
||||
47 | 'prefix' => 'v', |
||||
48 | 'longPrefix' => 'verbose', |
||||
49 | 'description' => 'Verbose output, shows GameOfLife in CLI', |
||||
50 | 'noValue' => true, |
||||
51 | ], |
||||
52 | 'help' => [ |
||||
53 | 'prefix' => 'h', |
||||
54 | 'longPrefix' => 'help', |
||||
55 | 'description' => 'Prints a usage statement', |
||||
56 | 'noValue' => true, |
||||
57 | ], |
||||
58 | ]); |
||||
59 | |||||
60 | $climate->description("Connway's Game of Life by Midori Kocak"); |
||||
61 | $climate->arguments->parse(); |
||||
62 | |||||
63 | try { |
||||
64 | if ($climate->arguments->defined('help')) { |
||||
65 | $climate->usage(); |
||||
66 | exit(); |
||||
67 | } |
||||
68 | |||||
69 | $verbose = false; |
||||
70 | |||||
71 | if ($climate->arguments->defined('verbose')) { |
||||
72 | $verbose = true; |
||||
73 | } |
||||
74 | |||||
75 | if ($climate->arguments->defined('filename')) { |
||||
76 | $filename = $climate->arguments->get('filename'); |
||||
77 | } elseif ($climate->arguments->defined('random')) { |
||||
78 | $exit = false; |
||||
79 | if (!$climate->arguments->defined('size')) { |
||||
80 | $climate->to('error')->red('Needs world --size or -s as a parameter'); |
||||
81 | $exit = true; |
||||
82 | } else { |
||||
83 | $size = $climate->arguments->get('size'); |
||||
84 | } |
||||
85 | |||||
86 | if (!$climate->arguments->defined('iterations')) { |
||||
87 | $climate->to('error')->red('Needs number of --iterations or -i as a parameter'); |
||||
88 | $exit = true; |
||||
89 | } else { |
||||
90 | $iterations = $climate->arguments->get('iterations'); |
||||
91 | } |
||||
92 | |||||
93 | if (!$climate->arguments->defined('species')) { |
||||
94 | $climate->to('error')->red('Needs number of --species or -sp as a parameter'); |
||||
95 | $exit = true; |
||||
96 | } else { |
||||
97 | $species = $climate->arguments->get('species'); |
||||
98 | } |
||||
99 | if ($exit === true) { |
||||
100 | $climate->usage(); |
||||
101 | exit(); |
||||
102 | } |
||||
103 | |||||
104 | $randomArray = GameOfLife::createRandomMatrix($size, $size, $species); |
||||
105 | |||||
106 | if ($climate->arguments->defined('filename')) { |
||||
107 | $filename = $climate->arguments->get('filename'); |
||||
108 | } else { |
||||
109 | $filename = 'data/random.xml'; |
||||
110 | } |
||||
111 | |||||
112 | GameOfLife::createXMLfromCells($filename, $randomArray, $species, $iterations); |
||||
113 | } |
||||
114 | |||||
115 | if ($climate->arguments->defined('outputFilename')) { |
||||
116 | $outputFilename = $filename = $climate->arguments->get('outputFilename'); |
||||
117 | if (empty($outputFilename)) { |
||||
118 | $outputFilename = "out.xml"; |
||||
119 | } |
||||
120 | $gameOfLife = new GameOfLife($filename, $outputFilename); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
$outputFilename can also be of type boolean ; however, parameter $outputFilename of MidoriKocak\GameOfLife\GameOfLife::__construct() does only seem to accept null|string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
121 | } elseif (isset($filename) || $climate->arguments->defined('filename')) { |
||||
122 | $gameOfLife = new GameOfLife($filename); |
||||
123 | } |
||||
124 | if (isset($gameOfLife)) { |
||||
125 | $gameOfLife->start($verbose); |
||||
126 | } else { |
||||
127 | $climate->usage(); |
||||
128 | exit(); |
||||
129 | } |
||||
130 | } catch (Exception $e) { |
||||
131 | $climate->to('error')->red($e->getMessage()); |
||||
132 | $climate->usage(); |
||||
133 | exit(); |
||||
134 | } |
||||
135 | |||||
136 |