Skip to content

Commit 2137a8b

Browse files
authored
Merge pull request #40 from bhushangaykawad/refactor/improve-coding-style
improved coding style and documentation
2 parents 05f9ac1 + 33f0702 commit 2137a8b

File tree

13 files changed

+123
-68
lines changed

13 files changed

+123
-68
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
vendor
33
composer.lock
4+
.php_cs.cache

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,6 @@ $result = batch()->insert($userInstance, $columns, $values, $batchSize);
144144
```
145145

146146
# Tests
147-
If you don't have phpunit installed on your project, first run `composser require phpunit/phpunit`
147+
If you don't have phpunit installed on your project, first run `composer require phpunit/phpunit`
148148

149149
In the root of your laravel app, run `./vendor/bin/phpunit ./vendor/mavinoo/laravel-batch/tests`

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"email": "mavin.developer@gmail.com"
1010
}
1111
],
12-
"require": {},
12+
"require": {
13+
"phpunit/phpunit": "^9.3@dev"
14+
},
1315
"autoload": {
1416
"psr-4": {
1517
"Mavinoo\\Batch\\": "src/"

src/Batch.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Mavinoo\Batch;
44

5-
use Illuminate\Database\DatabaseManager;
6-
use Illuminate\Database\Eloquent\Model;
75
use Mavinoo\Batch\Common\Common;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\DatabaseManager;
88

99
class Batch implements BatchInterface
1010
{
@@ -19,12 +19,14 @@ public function __construct(DatabaseManager $db)
1919
}
2020

2121
/**
22-
* Update multiple rows
22+
* Update multiple rows.
23+
*
2324
* @param Model $table
2425
* @param array $values
2526
* @param string $index
27+
* @param bool $raw
28+
* @return bool|int
2629
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
27-
*
2830
* @desc
2931
* Example
3032
* $table = 'users';
@@ -41,19 +43,17 @@ public function __construct(DatabaseManager $db)
4143
* ] ,
4244
* ];
4345
* $index = 'id';
44-
*
45-
* @return bool|int
4646
*/
4747
public function update(Model $table, array $values, string $index = null, bool $raw = false)
4848
{
4949
$final = [];
5050
$ids = [];
5151

52-
if (!count($values)) {
52+
if (! count($values)) {
5353
return false;
5454
}
5555

56-
if (!isset($index) || empty($index)) {
56+
if (! isset($index) || empty($index)) {
5757
$index = $table->getKeyName();
5858
}
5959

@@ -74,21 +74,21 @@ public function update(Model $table, array $values, string $index = null, bool $
7474
. 'ELSE `' . $k . '` END), ';
7575
}
7676

77-
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ");";
77+
$query = 'UPDATE `' . $this->getFullTableName($table) . '` SET ' . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '"' . ');';
7878

7979
return $this->db->connection($this->getConnectionName($table))->update($query);
8080
}
8181

8282
/**
83-
* Update multiple rows
83+
* Update multiple rows.
84+
*
8485
* @param Model $table
8586
* @param array $values
8687
* @param string $index
8788
* @param string|null $index2
8889
* @param bool $raw
8990
* @return bool|int
9091
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
91-
*
9292
* @desc
9393
* Example
9494
* $table = 'users';
@@ -106,45 +106,45 @@ public function update(Model $table, array $values, string $index = null, bool $
106106
* ];
107107
* $index = 'id';
108108
* $index2 = 'user_id';
109-
*
110109
*/
111110
public function updateWithTwoIndex(Model $table, array $values, string $index = null, string $index2 = null, bool $raw = false)
112111
{
113112
$final = [];
114113
$ids = [];
115114

116-
if (!count($values)) {
115+
if (! count($values)) {
117116
return false;
118117
}
119118

120-
if (!isset($index) || empty($index)) {
119+
if (! isset($index) || empty($index)) {
121120
$index = $table->getKeyName();
122121
}
123122

124123
foreach ($values as $key => $val) {
125-
$ids[] = $val[$index];
124+
$ids[] = $val[$index];
126125
$ids2[] = $val[$index2];
127126
foreach (array_keys($val) as $field) {
128-
if ($field !== $index || $field !== $index2 ) {
127+
if ($field !== $index || $field !== $index2) {
129128
$finalField = $raw ? Common::mysql_escape($val[$field]) : '"' . Common::mysql_escape($val[$field]) . '"';
130129
$value = (is_null($val[$field]) ? 'NULL' : $finalField);
131-
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) .'" AND `'. $index2 . '` = "' . $val[$index2] .'") THEN ' . $value . ' ';
130+
$final[$field][] = 'WHEN (`' . $index . '` = "' . Common::mysql_escape($val[$index]) . '" AND `' . $index2 . '` = "' . $val[$index2] . '") THEN ' . $value . ' ';
132131
}
133132
}
134133
}
135134

136135
$cases = '';
137136
foreach ($final as $k => $v) {
138137
$cases .= '`' . $k . '` = (CASE ' . implode("\n", $v) . "\n"
139-
. 'ELSE `' . $k . '` END), ';
138+
. 'ELSE `' . $k . '` END), ';
140139
}
141-
$query = "UPDATE `" . $this->getFullTableName($table) . "` SET " . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '")' . " AND `$index2` IN(" . '"' . implode('","', $ids2) . '"' ." );";
140+
$query = 'UPDATE `' . $this->getFullTableName($table) . '` SET ' . substr($cases, 0, -2) . " WHERE `$index` IN(" . '"' . implode('","', $ids) . '")' . " AND `$index2` IN(" . '"' . implode('","', $ids2) . '"' . ' );';
142141

143142
return $this->db->connection($this->getConnectionName($table))->update($query);
144143
}
145144

146145
/**
147-
* Insert Multi rows
146+
* Insert Multi rows.
147+
*
148148
* @param Model $table
149149
* @param array $columns
150150
* @param array $values
@@ -153,7 +153,6 @@ public function updateWithTwoIndex(Model $table, array $values, string $index =
153153
* @return bool|mixed
154154
* @throws \Throwable
155155
* @updatedBy Ibrahim Sakr <ebrahimes@gmail.com>
156-
*
157156
* @desc
158157
* Example
159158
*
@@ -194,7 +193,7 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
194193
{
195194
// no need for the old validation since we now use type hint that supports from php 7.0
196195
// but I kept this one
197-
if (count($columns) != count($values[0])) {
196+
if (count($columns) !== count($values[0])) {
198197
return false;
199198
}
200199

@@ -209,7 +208,7 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
209208
$values = array_chunk($values, $totalChunk, true);
210209

211210
foreach ($columns as $key => $column) {
212-
$columns[$key] = "`" . Common::mysql_escape($column) . "`";
211+
$columns[$key] = '`' . Common::mysql_escape($column) . '`';
213212
}
214213

215214
foreach ($values as $value) {
@@ -225,14 +224,13 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
225224

226225
$valueString = implode(', ', $valueArray);
227226

228-
$ignoreStmt = $insertIgnore ? ' IGNORE ' : '';
227+
$ignoreStmt = $insertIgnore ? ' IGNORE ' : '';
229228

230-
$query[] = "INSERT ".$ignoreStmt." INTO `" . $this->getFullTableName($table) . "` (" . implode(',', $columns) . ") VALUES $valueString;";
229+
$query[] = 'INSERT ' . $ignoreStmt . ' INTO `' . $this->getFullTableName($table) . '` (' . implode(',', $columns) . ") VALUES $valueString;";
231230
}
232231

233232
if (count($query)) {
234233
return $this->db->transaction(function () use ($totalValues, $totalChunk, $query, $table) {
235-
236234
$totalQuery = 0;
237235
foreach ($query as $value) {
238236
$totalQuery += $this->db->connection($this->getConnectionName($table))->statement($value) ? 1 : 0;
@@ -250,6 +248,8 @@ public function insert(Model $table, array $columns, array $values, int $batchSi
250248
}
251249

252250
/**
251+
* Get full table name.
252+
*
253253
* @param Model $model
254254
* @return string
255255
* @author Ibrahim Sakr <ebrahimes@gmail.com>
@@ -260,14 +260,17 @@ private function getFullTableName(Model $model)
260260
}
261261

262262
/**
263+
* Get connection name.
264+
*
263265
* @param Model $model
264266
* @return string|null
265267
* @author Ibrahim Sakr <ebrahimes@gmail.com>
266268
*/
267269
private function getConnectionName(Model $model)
268270
{
269-
if (!is_null($cn = $model->getConnectionName()))
271+
if (! is_null($cn = $model->getConnectionName())) {
270272
return $cn;
273+
}
271274

272275
return $model->getConnection()->getName();
273276
}

src/BatchFacade.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Mavinoo\Batch;
44

55
use Illuminate\Support\Facades\Facade;
66

77
class BatchFacade extends Facade
88
{
9+
/**
10+
* Get facade accessor to retrieve instance.
11+
*
12+
* @return string
13+
*/
914
protected static function getFacadeAccessor()
1015
{
11-
return 'Batch';
16+
return 'Batch';
1217
}
1318
}

src/BatchInterface.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Mavinoo\Batch;
44

55
use Illuminate\Database\Eloquent\Model;
66

77
interface BatchInterface
88
{
9+
/**
10+
* Update multiple rows.
11+
*
12+
* @param Model $table
13+
* @param array $values
14+
* @param string|null $index
15+
* @param bool $raw
16+
* @return mixed
17+
*/
918
public function update(Model $table, array $values, string $index = null, bool $raw = false);
1019

20+
/**
21+
* Update multiple rows with two index.
22+
*
23+
* @param Model $table
24+
* @param array $values
25+
* @param string|null $index
26+
* @param string|null $index2
27+
* @param bool $raw
28+
* @return mixed
29+
*/
1130
public function updateWithTwoIndex(Model $table, array $values, string $index = null, string $index2 = null, bool $raw = false);
1231

32+
/**
33+
* Insert multiple rows.
34+
*
35+
* @param Model $table
36+
* @param array $columns
37+
* @param array $values
38+
* @param int $batchSize
39+
* @param bool $insertIgnore
40+
* @return mixed
41+
*/
1342
public function insert(Model $table, array $columns, array $values, int $batchSize = 500, bool $insertIgnore = false);
1443
}

src/BatchServiceProvider.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Mavinoo\Batch;
44

55
use Illuminate\Support\ServiceProvider;
66
use Illuminate\Database\DatabaseManager;
7-
use Mavinoo\Batch\Batch;
87

98
class BatchServiceProvider extends ServiceProvider
109
{
1110
/**
11+
* Register Batch instance to IOC.
12+
*
1213
* @updateedBy Ibrahim Sakr <ebrahimes@gmail.com>
1314
*/
1415
public function register()
1516
{
16-
$this->app->bind('Batch', function ($app){
17+
$this->app->bind('Batch', function ($app) {
1718
return new Batch($app->make(DatabaseManager::class));
1819
});
1920
}

src/Common/Common.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Mavinoo\Batch\Common;
44

55
class Common
66
{
7-
public static function mysql_escape($inp)
7+
/**
8+
* Escape values according to mysql.
9+
*
10+
* @param $fieldValue
11+
* @return array|string|string[]
12+
*/
13+
public static function mysql_escape($fieldValue)
814
{
9-
if(is_array($inp)) return array_map(__METHOD__, $inp);
15+
if (is_array($fieldValue)) {
16+
return array_map(__METHOD__, $fieldValue);
17+
}
1018

11-
if(!empty($inp) && is_string($inp))
12-
{
19+
if (! empty($fieldValue) && is_string($fieldValue)) {
1320
return str_replace(
1421
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
1522
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],
16-
$inp);
23+
$fieldValue
24+
);
1725
}
1826

19-
return $inp;
27+
return $fieldValue;
2028
}
2129
}

src/Common/Helpers.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

3-
if (! function_exists('batch'))
4-
{
3+
if (! function_exists('batch')) {
4+
/**
5+
* Batch helper to get Mavino\Batch\Batch instance.
6+
*
7+
* @return mixed
8+
*/
59
function batch()
610
{
711
return app('Mavinoo\Batch\Batch');

0 commit comments

Comments
 (0)