1 | <?php namespace Hafiz\Libraries; |
||||||
2 | |||||||
3 | use CodeIgniter\CLI\CLI; |
||||||
4 | use Config\Services; |
||||||
5 | |||||||
6 | class FileHandler |
||||||
7 | { |
||||||
8 | /** |
||||||
9 | * @param string $table |
||||||
10 | * @param string $fields |
||||||
11 | * @param string $keys |
||||||
12 | */ |
||||||
13 | public function writeTable(string $table, string $fields, string $keys) |
||||||
14 | { |
||||||
15 | helper('inflector'); |
||||||
16 | $fileName = date('Y-m-d-His') . '_create_' . $table . '_table.php'; |
||||||
17 | $targetDir = ROOTPATH . 'app/Database/Migrations/'; |
||||||
18 | $filePath = $targetDir . $fileName; |
||||||
19 | |||||||
20 | $replace = ['{migrate}', '{fields}', '{keys}', '{table}']; |
||||||
21 | |||||||
22 | $with = [$fields, $keys, $table]; |
||||||
23 | $newTemplate = $this->migrationTemplate(); |
||||||
24 | |||||||
25 | $finalFile = str_replace($replace, $with, $newTemplate); |
||||||
26 | file_put_contents($filePath, $finalFile); |
||||||
27 | } |
||||||
28 | |||||||
29 | /** |
||||||
30 | * @return string |
||||||
31 | */ |
||||||
32 | protected function migrationTemplate() |
||||||
33 | { |
||||||
34 | return "<?php namespace App\Database\Migrations;\n\n" . |
||||||
35 | "use CodeIgniter\Database\Migration;\n\n" . |
||||||
36 | "/*\n" . |
||||||
37 | " * @class {migrate} Migration\n" . |
||||||
38 | " * @author Mohammad Hafijul Islam <[email protected]>\n" . |
||||||
39 | " * @license Free to use and abuse\n" . |
||||||
40 | " * @version 1.0.0 Beta\n" . |
||||||
41 | " * @created_by SQL2Migration Generator\n" . |
||||||
42 | " * @created_at " . date("d F, Y h:i:s A") . "\n" . |
||||||
43 | " */\n\n" . |
||||||
44 | "class Create{migrate} extends Migration\n" . |
||||||
45 | "{\n\n\tpublic function up() \n\t{\n\t\t" . |
||||||
46 | "\$this->forge->addField([" . |
||||||
47 | "\t\t\t{fields}\n" . |
||||||
48 | "\t]);\n\n\n\t\t" . |
||||||
49 | "\t\t{keys}\n" . |
||||||
50 | "\t\t\$this->forge->createTable('{table}');\n" . |
||||||
51 | "\n\t}\n\n" . |
||||||
52 | "\t//--------------------------------------------------------------------\n" . |
||||||
53 | "\n\tpublic function down()\n" . |
||||||
54 | "\t{\n" . |
||||||
55 | "\t\t\$this->forge->dropTable('{table}');\n" . |
||||||
56 | "\t}\n}"; |
||||||
57 | } |
||||||
58 | |||||||
59 | /** |
||||||
60 | * @param string $path |
||||||
61 | * @return bool |
||||||
62 | */ |
||||||
63 | public function checkFileExist(string $path): bool |
||||||
64 | { |
||||||
65 | if (is_file($path)) { |
||||||
66 | $permission = CLI::prompt("File already exists.Overwrite? ", ['yes', 'no'], 'required|in_list[yes,no]'); |
||||||
67 | if ($permission == 'no') { |
||||||
68 | CLI::error("Task Cancelled."); |
||||||
69 | exit(1); |
||||||
0 ignored issues
–
show
|
|||||||
70 | } |
||||||
71 | } |
||||||
72 | return true; |
||||||
73 | } |
||||||
74 | |||||||
75 | /** |
||||||
76 | * Create new Directory and verify |
||||||
77 | * if that already exist or not |
||||||
78 | * |
||||||
79 | * @param string $path |
||||||
80 | * @return bool |
||||||
81 | */ |
||||||
82 | public function verifyDirectory(string $path = ''): bool |
||||||
83 | { |
||||||
84 | $targetDir = $this->pathFromLocation($path); |
||||||
85 | $permission = null; |
||||||
0 ignored issues
–
show
|
|||||||
86 | |||||||
87 | //if folder already exists |
||||||
88 | if ($this->checkFolderExist($path) == true) |
||||||
0 ignored issues
–
show
|
|||||||
89 | return true; |
||||||
90 | |||||||
91 | //create a folder |
||||||
92 | if (!mkdir($targetDir, 0755, true) == true) { |
||||||
0 ignored issues
–
show
|
|||||||
93 | CLI::error("Directory Location is not writable."); |
||||||
94 | return false; |
||||||
95 | } |
||||||
96 | |||||||
97 | return true; |
||||||
98 | } |
||||||
99 | |||||||
100 | /** |
||||||
101 | * Take a File Location with file name then remove file name |
||||||
102 | * return a Directory Location |
||||||
103 | * |
||||||
104 | * @param string $filePath |
||||||
105 | * @return string |
||||||
106 | */ |
||||||
107 | protected function pathFromLocation(string $filePath): string |
||||||
108 | { |
||||||
109 | return dirname($filePath); |
||||||
110 | } |
||||||
111 | |||||||
112 | /** |
||||||
113 | * @param string $path |
||||||
114 | * @return bool |
||||||
115 | */ |
||||||
116 | public function checkFolderExist(string $path = ''): bool |
||||||
117 | { |
||||||
118 | $path = realpath($this->pathFromLocation($path)); |
||||||
119 | |||||||
120 | if (!is_dir($path)) { |
||||||
121 | CLI::error("Directory: " . $path . " is Invalid or doesn't Exists."); |
||||||
122 | |||||||
123 | return false; |
||||||
124 | } |
||||||
125 | |||||||
126 | return true; |
||||||
127 | } |
||||||
128 | |||||||
129 | /** |
||||||
130 | * Given a Namespace name detect Real path |
||||||
131 | * Or return the Path of Default once. |
||||||
132 | * @param string $space |
||||||
133 | * @param string $default |
||||||
134 | * @return array $pathinfo |
||||||
135 | */ |
||||||
136 | public function getNamespaceInfo($space = null, string $default = 'App'): array |
||||||
137 | { |
||||||
138 | // Get all namespaces |
||||||
139 | $namespaces = Services::autoloader()->getNamespace(); |
||||||
140 | |||||||
141 | if (!is_null($space)) { |
||||||
142 | if (key_exists($space, $namespaces)) { |
||||||
143 | return ['ns' => $space, |
||||||
144 | 'path' => realpath(reset($namespaces[$space])), |
||||||
145 | 'default' => false]; |
||||||
146 | } |
||||||
147 | CLI::error("Namespace not found in AutoLoader. Using Default [$default]"); |
||||||
148 | } |
||||||
149 | |||||||
150 | return ['ns' => $default, |
||||||
151 | 'path' => realpath(reset($namespaces[$default])), |
||||||
152 | 'default' => true]; |
||||||
153 | } |
||||||
154 | |||||||
155 | /** |
||||||
156 | * @param string $parent |
||||||
157 | * @param string $default |
||||||
158 | * @return array |
||||||
159 | */ |
||||||
160 | public function getParentNamespace(string $parent, string $default): array |
||||||
0 ignored issues
–
show
The parameter
$default is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() The parameter
$parent is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||||
161 | { |
||||||
162 | |||||||
163 | } |
||||||
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return array . Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
![]() |
|||||||
164 | |||||||
165 | /** |
||||||
166 | * @param string $template |
||||||
167 | * @param array $data |
||||||
168 | * @return string |
||||||
169 | */ |
||||||
170 | public function renderTemplate(string $template, array $data): string |
||||||
171 | { |
||||||
172 | $templateDir = realpath(__DIR__ . '/../Templates/') . '/'; |
||||||
173 | $skeleton = file_get_contents($templateDir . $template . '.php'); |
||||||
174 | |||||||
175 | return str_replace(array_keys($data), array_values($data), $skeleton); |
||||||
176 | } |
||||||
177 | } |
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: