Skip to content

Commit 5a57798

Browse files
committed
Remove LoggerInterface::batchOperationResults
Because it produces noisy outputs and make the code complicated.
1 parent ebc4d3f commit 5a57798

File tree

7 files changed

+25
-160
lines changed

7 files changed

+25
-160
lines changed

src/CLI/Logger.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -152,27 +152,4 @@ public function log($level, $message, array $_context = []): void
152152
{
153153
WP_CLI::log('[' . $level . ']' . $message);
154154
}
155-
156-
/**
157-
* Report the results of the same operation against multiple resources.
158-
*
159-
* @param string $noun Resource being affected (e.g. plugin).
160-
* @param string $verb Type of action happening to the noun (e.g. activate).
161-
* @param integer $total Total number of resource being affected.
162-
* @param integer $successes Number of successful operations.
163-
* @param integer $failures Number of failures.
164-
* @param null|integer $skips Optional. Number of skipped operations. Default null (don't show skips).
165-
*
166-
* @return void
167-
*/
168-
public function batchOperationResults(
169-
string $noun,
170-
string $verb,
171-
int $total,
172-
int $successes,
173-
int $failures,
174-
?int $skips = null
175-
): void {
176-
report_batch_operation_results($noun, $verb, $total, $successes, $failures, $skips);
177-
}
178155
}

src/LoggerInterface.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,4 @@ interface LoggerInterface extends BaseLoggerInterface
1515
* @return void
1616
*/
1717
public function section($title): void;
18-
19-
/**
20-
* Report the results of the same operation against multiple resources.
21-
*
22-
* @param string $noun Resource being affected (e.g. plugin).
23-
* @param string $verb Type of action happening to the noun (e.g. activate).
24-
* @param integer $total Total number of resource being affected.
25-
* @param integer $successes Number of successful operations.
26-
* @param integer $failures Number of failures.
27-
* @param null|integer $skips Optional. Number of skipped operations. Default null (don't show skips).
28-
*
29-
* @return void
30-
*/
31-
public function batchOperationResults(
32-
string $noun,
33-
string $verb,
34-
int $total,
35-
int $successes,
36-
int $failures,
37-
?int $skips = null
38-
): void;
3918
}

src/Operations/AttachmentImages/Optimize.php

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@
1010
use TypistTech\ImageOptimizeCommand\Repositories\AttachmentRepository;
1111
use function WP_CLI\Utils\normalize_path;
1212

13-
/**
14-
* TODO: Refactor this class.
15-
*/
1613
class Optimize
1714
{
18-
protected const SUCCESS = 0;
19-
protected const ERROR = 1;
20-
2115
/**
2216
* The logger.
2317
*
@@ -77,38 +71,22 @@ public function execute(int ...$ids): void
7771
});
7872
$ids = array_filter($ids);
7973

80-
$results = array_map(function (int $id): array {
74+
array_map(function (int $id): void {
8175
// phpcs:ignore
82-
return $this->optimizeAttachment($id);
76+
$this->optimizeAttachment($id);
8377
}, $ids);
8478

85-
$results = $this->flattenResults($results);
86-
87-
$successes = count(array_filter($results, function (int $result): bool {
88-
return static::SUCCESS === $result;
89-
}));
90-
91-
$failures = count(array_filter($results, function (int $result): bool {
92-
return static::ERROR === $result;
93-
}));
94-
95-
$this->logger->batchOperationResults(
96-
'image',
97-
'optimize',
98-
count($results),
99-
$successes,
100-
$failures
101-
);
79+
$this->logger->info('Finished');
10280
}
10381

10482
/**
10583
* Optimize all images of an attachment.
10684
*
10785
* @param int $id The attachment ID.
10886
*
109-
* @return int[]
87+
* @return void
11088
*/
111-
protected function optimizeAttachment(int $id): array
89+
protected function optimizeAttachment(int $id): void
11290
{
11391
$this->logger->debug('Optimizing images of attachment ID: ' . $id);
11492

@@ -118,72 +96,51 @@ protected function optimizeAttachment(int $id): array
11896
return normalize_path($path);
11997
}, $paths);
12098

121-
$results = array_map(function (string $imagePath): int {
99+
$results = array_map(function (string $imagePath): bool {
122100
// phpcs:ignore
123101
return $this->optimizeImage($imagePath);
124102
}, $normalizedPaths);
125103

126-
if (in_array(static::SUCCESS, $results, true)) {
104+
if (in_array(true, $results, true)) {
127105
$this->logger->debug('Marking attachment ID: ' . $id . ' as optimized.');
128106
$this->repo->markAsOptimized($id);
129107
$this->logger->notice('Optimized images of attachment ID: ' . $id);
130108
}
131-
132-
return $results;
133109
}
134110

135111
/**
136112
* Optimize an image.
137113
*
138114
* @param string $path Path to the image.
139115
*
140-
* @return int
116+
* @return bool
141117
*/
142-
protected function optimizeImage(string $path): int
118+
protected function optimizeImage(string $path): bool
143119
{
144120
try {
145121
$this->logger->debug('Optimizing image - ' . $path);
146122

147123
if (! is_readable($path)) {
148124
$this->logger->error('Image not readable - ' . $path);
149125

150-
return static::ERROR;
126+
return false;
151127
}
152128

153129
// phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
154130
if (! is_writable($path)) {
155131
$this->logger->error('Image not writable - ' . $path);
156132

157-
return static::ERROR;
133+
return false;
158134
}
159135

160136
$this->optimizerChain->optimize($path);
161137

162-
return static::SUCCESS;
138+
return true;
163139
// phpcs:ignore
164140
} catch (IOException $exception) {
165141
$this->logger->error('Failed to optimize ' . $path);
166142

167-
return static::ERROR;
168-
}
169-
}
170-
171-
/**
172-
* Flatten result arrays.
173-
*
174-
* @param array $results Array of result arrays.
175-
*
176-
* @return int[]
177-
*/
178-
protected function flattenResults(array $results): array
179-
{
180-
switch (count($results)) {
181-
case 0:
182-
return [];
183-
case 1:
184-
return $results[0];
185-
default:
186-
return array_merge(...$results);
143+
return false;
187144
}
188145
}
189146
}

src/Operations/AttachmentImages/Restore.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,22 @@ public function execute(int ...$ids): void
6363
$total = count($ids);
6464
$this->logger->section('Restoring full sized images for ' . $total . ' attachment(s)');
6565

66-
$results = array_map(function (int $id): int {
66+
array_map(function (int $id): void {
6767
// phpcs:ignore
68-
return $this->restore($id);
68+
$this->restore($id);
6969
}, $ids);
7070

71-
$successes = count(array_filter($results, function (int $result): bool {
72-
return static::SUCCESS === $result;
73-
}));
74-
75-
$failures = count(array_filter($results, function (int $result): bool {
76-
return static::ERROR === $result;
77-
}));
78-
79-
$this->logger->batchOperationResults('full sized image', 'restore', $total, $successes, $failures);
71+
$this->logger->info('Finished');
8072
}
8173

8274
/**
8375
* Override an attachment full sized image with its `.original` version.
8476
*
8577
* @param int $id The attachment ID.
8678
*
87-
* @return int
79+
* @return void
8880
*/
89-
protected function restore(int $id): int
81+
protected function restore(int $id): void
9082
{
9183
try {
9284
$this->logger->debug('Restoring attachment ID: ' . $id);
@@ -96,8 +88,7 @@ protected function restore(int $id): int
9688

9789
if (empty($path)) {
9890
$this->logger->error('Full sized image not found for attachment ID: ' . $id);
99-
100-
return static::ERROR;
91+
return;
10192
}
10293

10394
$path = normalize_path($path);
@@ -111,12 +102,9 @@ protected function restore(int $id): int
111102
$this->repo->markAsNonOptimized($id);
112103
$this->logger->notice('Restored attachment ID: ' . $id);
113104

114-
return static::SUCCESS;
115105
// phpcs:ignore
116106
} catch (IOException $exception) {
117107
$this->logger->error('Failed to restore full sized image for attachment ID: ' . $id);
118-
119-
return static::ERROR;
120108
}
121109
}
122110
}

src/Operations/Backup.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,34 +57,22 @@ public function execute(string ...$paths): void
5757
return normalize_path($path);
5858
}, $paths);
5959

60-
$results = array_map(function (string $path): int {
60+
array_map(function (string $path): void {
6161
// phpcs:ignore
62-
return $this->backup($path);
62+
$this->backup($path);
6363
}, $normalizedPaths);
6464

65-
$successes = count(array_filter($results, function (int $result): bool {
66-
return static::SUCCESS === $result;
67-
}));
68-
69-
$failures = count(array_filter($results, function (int $result): bool {
70-
return static::ERROR === $result;
71-
}));
72-
73-
$skips = count(array_filter($results, function (int $result): bool {
74-
return static::SKIP === $result;
75-
}));
76-
77-
$this->logger->batchOperationResults('full sized image', 'backup', $total, $successes, $failures, $skips);
65+
$this->logger->info('Finished');
7866
}
7967

8068
/**
8169
* Backup a file at its directory with `.original` extension.
8270
*
8371
* @param string $path Path to the file to be backed up.
8472
*
85-
* @return int
73+
* @return void
8674
*/
87-
protected function backup(string $path): int
75+
protected function backup(string $path): void
8876
{
8977
try {
9078
$this->logger->debug('Backing up full sized image - ' . $path);
@@ -93,18 +81,15 @@ protected function backup(string $path): int
9381
if ($isBackupExists) {
9482
$this->logger->debug('Skip: Backup already exists - ' . $path);
9583

96-
return static::SKIP;
84+
return;
9785
}
9886

9987
$this->filesystem->copy($path, $path . static::ORIGINAL_EXTENSION);
10088
$this->logger->notice('Backed up full sized image - ' . $path);
10189

102-
return static::SUCCESS;
10390
// phpcs:ignore
10491
} catch (IOException | FileNotFoundException $exception) {
10592
$this->logger->error('Failed to backup ' . $path);
106-
107-
return static::ERROR;
10893
}
10994
}
11095
}

tests/unit/Operations/AttachmentImages/RestoreTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public function testRestoreSuccess()
5353
$logger->shouldHaveReceived('section')
5454
->with('Restoring full sized images for 1 attachment(s)')
5555
->once();
56-
$logger->shouldHaveReceived('batchOperationResults')
57-
->with('full sized image', 'restore', 1, 1, 0)
58-
->once();
5956

6057
$this->filesystem->seeFileFound('restore-me.txt', $this->testDir);
6158
$this->filesystem->dontSeeFileFound('restore-me.txt.original', $this->testDir);
@@ -88,9 +85,6 @@ public function testRestoreNotExistBackup()
8885
$logger->shouldHaveReceived('section')
8986
->with('Restoring full sized images for 1 attachment(s)')
9087
->once();
91-
$logger->shouldHaveReceived('batchOperationResults')
92-
->with('full sized image', 'restore', 1, 0, 1)
93-
->once();
9488

9589
$this->filesystem->dontSeeFileFound('not-exist.png', $this->testDir);
9690
$this->filesystem->dontSeeFileFound('not-exist.png.original', $this->testDir);
@@ -122,9 +116,6 @@ public function testRestoreNotAttachment()
122116
$logger->shouldHaveReceived('error')
123117
->with('Full sized image not found for attachment ID: 123')
124118
->once();
125-
$logger->shouldHaveReceived('batchOperationResults')
126-
->with('full sized image', 'restore', 1, 0, 1)
127-
->once();
128119
}
129120

130121
protected function _before()

tests/unit/Operations/BackupTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public function testBackupSuccess()
4343
$logger->shouldHaveReceived('section')
4444
->with('Backing up 1 full sized image(s)')
4545
->once();
46-
$logger->shouldHaveReceived('batchOperationResults')
47-
->with('full sized image', 'backup', 1, 1, 0, 0)
48-
->once();
4946
}
5047

5148
public function testBackupOriginalNotExist()
@@ -64,9 +61,6 @@ public function testBackupOriginalNotExist()
6461
$logger->shouldHaveReceived('section')
6562
->with('Backing up 1 full sized image(s)')
6663
->once();
67-
$logger->shouldHaveReceived('batchOperationResults')
68-
->with('full sized image', 'backup', 1, 0, 1, 0)
69-
->once();
7064
}
7165

7266
public function testBackupAlreadyExist()
@@ -85,9 +79,6 @@ public function testBackupAlreadyExist()
8579
$logger->shouldHaveReceived('section')
8680
->with('Backing up 1 full sized image(s)')
8781
->once();
88-
$logger->shouldHaveReceived('batchOperationResults')
89-
->with('full sized image', 'backup', 1, 0, 0, 1)
90-
->once();
9182
}
9283

9384
public function testBackup()
@@ -114,9 +105,6 @@ public function testBackup()
114105
$logger->shouldHaveReceived('section')
115106
->with('Backing up 3 full sized image(s)')
116107
->once();
117-
$logger->shouldHaveReceived('batchOperationResults')
118-
->with('full sized image', 'backup', 3, 1, 1, 1)
119-
->once();
120108
}
121109

122110
protected function _before()

0 commit comments

Comments
 (0)