• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dg / dresscode / 42928114183

10 Nov 2026 11:30PM UTC coverage: 97.596% (+0.6%) from 97.024%
42928114183

push

github

dg
released version 1.4.0

27609 of 28289 relevant lines covered (97.6%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.98
/src/Engine/WorkerPool.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the DressCode, a coding style and upgrade tool for PHP (https://dresscode.run)
5
 * Copyright (c) 2026 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace DressCode\Engine;
9

10
use DressCode\FileResult;
11
use function array_key_exists, count, function_exists, in_array, is_array, strlen;
12
use const PHP_OS_FAMILY, PHP_SAPI;
13

14

15
/**
16
 * Processes files in worker processes: a TCP server on the loopback hands the paths out one at a time,
17
 * a worker sends each result back as a line of JSON. The parent keeps the cache and the baseline, a worker
18
 * only processes and, when fixing, writes.
19
 * @internal
20
 */
21
final class WorkerPool
22
{
23
        /** microseconds the wait for the workers lasts at most while the progress is watched, so that a spinner turns */
24
        private const ProgressInterval = 100_000;
25

26
        /** seconds the end of a worker is waited for once its connection has closed */
27
        private const EndTimeout = 1;
28

29
        /**
30
         * The listening socket: from ext/sockets when available, because the stream layer on Windows waits 500 ms
31
         * for a socket to become writable before closing it, which a listening one never does.
32
         * @var \Socket|resource|null
33
         */
34
        private mixed $server = null;
35

36
        /** @var array<int, resource>  connected workers by socket id */
37
        private array $sockets = [];
38

39
        /** @var array<int, string>  what a worker sent after its last complete line, by socket id */
40
        private array $buffers = [];
41

42
        /** workers that have connected so far */
43
        private int $accepted = 0;
44

45
        /** workers started once the first has connected */
46
        private int $pending = 0;
47

48
        /** the address the workers connect to */
49
        private string $address = '';
50

51
        /** @var array<int, array{string, float, string}>  path in progress with the time it started and its content, by socket id */
52
        private array $inProgress = [];
53

54
        /** @var ?\Closure(string): string  path => content, read when the path is handed out */
55
        private ?\Closure $read = null;
56

57
        /** @var list<array{resource, string}>  process, file with its output */
58
        private array $workers = [];
59

60
        /** when the last worker was started */
61
        private float $spawnedAt = 0;
62

63
        /** @var list<string> */
64
        private array $queue = [];
65

66
        /** @var array<string, true>  the files the results depend on, as the workers sent them */
67
        private array $dependencies = [];
68

69
        /** @var array<string, true>  what the analyses of the workers tell the user about the run */
70
        private array $warnings = [];
71

72

73
        public function __construct(
74
                /** @var list<string> the worker command line; --worker with the address is appended */
75
                private readonly array $command,
1 ✔
76
                private readonly int $jobs,
1 ✔
77
                /** working directory of the workers */
78
                private readonly ?string $cwd = null,
1 ✔
79
                /** seconds a worker may spend on one file before the run fails, since a rule has most likely looped */
80
                private readonly int $taskTimeout = 300,
1 ✔
81
                /** seconds a worker may take to connect, the first one filling the cache the others share among them */
82
                private readonly int $connectTimeout = 300,
1 ✔
83
                /** the first worker fills a cache the others share before it connects, and they start after that */
84
                private readonly bool $warmFirst = false,
1 ✔
85
        ) {
86
        }
1 ✔
87

88

89
        /** Processors the machine has, 1 when unknown. */
90
        public static function detectCpuCount(): int
91
        {
92
                $count = (int) getenv('NUMBER_OF_PROCESSORS');
1 ✔
93
                if ($count < 1 && is_readable('/proc/cpuinfo')) {
1 ✔
94
                        $count = preg_match_all('~^processor\s*:~m', (string) file_get_contents('/proc/cpuinfo'));
1 ✔
95
                }
96

97
                if ($count < 1 && in_array(PHP_OS_FAMILY, ['Darwin', 'BSD'], true) && function_exists('shell_exec')) {
1 ✔
98
                        $count = (int) @shell_exec('sysctl -n hw.ncpu 2>/dev/null'); // @ a failed command gives no count
×
99
                }
100

101
                return max(1, $count);
1 ✔
102
        }
103

104

105
        /**
106
         * The PHP a process of DressCode is started with: the same binary with the same ini file, the binary alone loading the
107
         * default one, and the settings the process has beyond it; null where no CLI binary is found to start one.
108
         * @return ?list<string>
109
         */
110
        public static function buildPhpCommand(): ?array
111
        {
112
                $binary = self::findCliBinary();
1 ✔
113
                if ($binary === null) {
1 ✔
114
                        return null;
×
115
                }
116

117
                $ini = php_ini_loaded_file();
1 ✔
118
                $php = [$binary, ...($ini === false ? (php_ini_scanned_files() === false ? ['-n'] : []) : ['-c', $ini])];
1 ✔
119
                return [...$php, ...self::findIniOverrides($php)];
1 ✔
120
        }
121

122

123
        /** The binary of the process, or under another SAPI than CLI, such as CGI, the CLI binary beside it. */
124
        private static function findCliBinary(): ?string
125
        {
126
                if (PHP_SAPI === 'cli') {
1 ✔
127
                        return PHP_BINARY;
1 ✔
128
                }
129

130
                $path = dirname(PHP_BINARY) . '/php' . (PHP_OS_FAMILY === 'Windows' ? '.exe' : '');
×
131
                return is_file($path) ? $path : null;
×
132
        }
133

134

135
        /**
136
         * The settings the process has and the PHP of the command alone would not, those given by -d above all, as the
137
         * options -d that give them to a worker; PHP tells no process the options it started with, so the PHP of the
138
         * command is asked for its settings and they are compared.
139
         * @param  list<string>  $php
140
         * @return list<string>
141
         */
142
        private static function findIniOverrides(array $php): array
143
        {
144
                $null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
1 ✔
145
                $process = @proc_open([...$php, '-r', 'echo serialize(ini_get_all(null, false));'], [1 => ['pipe', 'w'], 2 => ['file', $null, 'w']], $pipes); // @ no settings then
1 ✔
146
                if ($process === false) {
1 ✔
147
                        return [];
×
148
                }
149

150
                $base = @unserialize((string) stream_get_contents($pipes[1])); // @ a PHP that failed to start gives none
1 ✔
151
                fclose($pipes[1]);
1 ✔
152
                proc_close($process);
1 ✔
153
                $own = ini_get_all(details: false);
1 ✔
154
                if (!is_array($base) || $own === false) {
1 ✔
155
                        return [];
×
156
                }
157

158
                $options = [];
1 ✔
159
                foreach ($own as $name => $value) {
1 ✔
160
                        if (array_key_exists($name, $base) && $base[$name] !== $value) {
1 ✔
161
                                $options[] = '-d';
1 ✔
162
                                $options[] = "$name=$value";
1 ✔
163
                        }
164
                }
165

166
                return $options;
1 ✔
167
        }
168

169

170
        /**
171
         * The results by path, each as soon as it arrives; the content of a file is read when it is handed out, so that
172
         * only the files in progress are held.
173
         * @param  list<string>  $paths
174
         * @param  \Closure(string): string  $read  path => content
175
         * @param  ?\Closure(int, array<string, float>): void  $onProgress  files done and the paths in progress
176
         * @return \Generator<string, FileResult>
177
         * @throws \RuntimeException  when a worker fails
178
         */
179
        public function process(array $paths, \Closure $read, ?\Closure $onProgress = null): \Generator
1 ✔
180
        {
181
                $address = $this->listen();
1 ✔
182
                $this->queue = $paths;
1 ✔
183
                $this->read = $read;
1 ✔
184
                $done = 0;
1 ✔
185
                try {
186
                        $count = min($this->jobs, count($this->queue));
1 ✔
187
                        $this->address = $address;
1 ✔
188
                        $this->pending = $this->warmFirst ? $count - 1 : 0;
1 ✔
189
                        for ($i = $count - $this->pending; $i > 0; $i--) {
1 ✔
190
                                $this->workers[] = $this->spawn($address);
1 ✔
191
                        }
192

193
                        while ($done < count($paths)) {
1 ✔
194
                                foreach ($this->await(watched: $onProgress !== null) as $stream) {
1 ✔
195
                                        foreach ($this->receive((int) $stream) as $path => $result) {
1 ✔
196
                                                $done++;
1 ✔
197
                                                yield $path => $result;
1 ✔
198
                                        }
199
                                }
200

201
                                $this->checkWorkers($done < count($paths));
1 ✔
202
                                if ($onProgress !== null) {
1 ✔
203
                                        $onProgress($done, $this->getInProgress());
×
204
                                }
205
                        }
206

207
                } finally {
1 ✔
208
                        foreach ($this->sockets as $socket) {
1 ✔
209
                                fclose($socket);
1 ✔
210
                        }
211

212
                        $this->closeServer();
1 ✔
213
                        $this->stop();
1 ✔
214
                }
215
        }
1 ✔
216

217

218
        /**
219
         * The files the results of the workers depend on beyond the configuration, as they sent them.
220
         * @return list<string>
221
         */
222
        public function getDependencies(): array
223
        {
224
                return array_keys($this->dependencies);
1 ✔
225
        }
226

227

228
        /**
229
         * What the analyses of the workers tell the user about the run, each thing once.
230
         * @return list<string>
231
         */
232
        public function getWarnings(): array
233
        {
234
                return array_keys($this->warnings);
1 ✔
235
        }
236

237

238
        /** @return string  the address the workers connect to */
239
        private function listen(): string
240
        {
241
                if (function_exists('socket_create')) {
1 ✔
242
                        $server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
1 ✔
243
                        if (
244
                                $server === false
1 ✔
245
                                || !socket_bind($server, '127.0.0.1')
1 ✔
246
                                || !socket_listen($server, $this->jobs)
1 ✔
247
                                || !socket_getsockname($server, $ip, $port)
1 ✔
248
                        ) {
249
                                throw new \RuntimeException('Cannot start the worker server: ' . socket_strerror(socket_last_error()));
×
250
                        }
251

252
                        $this->server = $server;
1 ✔
253
                        return "$ip:$port";
1 ✔
254
                }
255

256
                $context = stream_context_create(['socket' => ['tcp_nodelay' => true]]); // Nagle would delay every small message by an ACK
×
257
                $server = @stream_socket_server('tcp://127.0.0.1:0', $errno, $error, context: $context); // @ is escalated to exception
×
258
                if ($server === false) {
×
259
                        throw new \RuntimeException("Cannot start the worker server: $error");
×
260
                }
261

262
                $this->server = $server;
×
263
                return (string) stream_socket_get_name($server, remote: false);
×
264
        }
265

266

267
        /**
268
         * Waits for messages from the workers, accepting the ones that connect meanwhile; the listening socket
269
         * from ext/sockets is closed as soon as the last worker is in.
270
         * @param  bool  $watched  the progress is drawn, so the wait is short
271
         * @return list<resource>  connections with a message waiting
272
         */
273
        private function await(bool $watched): array
274
        {
275
                $read = array_values($this->sockets);
1 ✔
276
                $write = $except = [];
1 ✔
277
                $seconds = $watched ? 0 : 1;
1 ✔
278
                $microseconds = $watched ? self::ProgressInterval : 0;
1 ✔
279
                $server = $this->server;
1 ✔
280
                if ($server instanceof \Socket) {
1 ✔
281
                        $this->acceptPending($server, blocking: $read === []);
1 ✔
282
                        if ($this->accepted === count($this->workers)) {
1 ✔
283
                                $this->closeServer();
1 ✔
284
                        } else { // short waits while workers may still be connecting
285
                                $seconds = 0;
1 ✔
286
                                $microseconds = 10_000;
1 ✔
287
                        }
288
                } elseif ($server !== null) {
1 ✔
289
                        $read[] = $server;
×
290
                }
291

292
                if ($read === []) { // nothing connected: the workers are still starting, or they are gone
1 ✔
293
                        return [];
1 ✔
294
                }
295

296
                if (@stream_select($read, $write, $except, $seconds, $microseconds) === false) { // @ is escalated to exception
1 ✔
297
                        throw new \RuntimeException('Waiting for the workers failed.');
×
298
                }
299

300
                $streams = [];
1 ✔
301
                foreach ($read as $stream) {
1 ✔
302
                        if ($stream === $server) {
1 ✔
303
                                $this->acceptStream($stream);
×
304
                        } else {
305
                                $streams[] = $stream;
1 ✔
306
                        }
307
                }
308

309
                return $streams;
1 ✔
310
        }
311

312

313
        /** Accepts every worker waiting to connect; with nothing to do otherwise, waits for the first one. */
314
        private function acceptPending(\Socket $server, bool $blocking): void
315
        {
316
                while (true) {
1 ✔
317
                        $read = [$server];
1 ✔
318
                        $write = $except = null;
1 ✔
319
                        if (socket_select($read, $write, $except, $blocking ? 1 : 0) !== 1) {
1 ✔
320
                                return;
1 ✔
321
                        }
322

323
                        $blocking = false;
1 ✔
324
                        $connection = socket_accept($server);
1 ✔
325
                        if ($connection === false) {
1 ✔
326
                                return;
×
327
                        }
328

329
                        socket_set_option($connection, SOL_TCP, TCP_NODELAY, 1); // Nagle would delay every small message by an ACK
1 ✔
330
                        $stream = socket_export_stream($connection);
1 ✔
331
                        if ($stream === false) {
1 ✔
332
                                throw new \RuntimeException('Cannot use the connection of a worker.');
×
333
                        }
334

335
                        $this->acceptConnection($stream);
1 ✔
336
                }
337
        }
×
338

339

340
        /** @param resource $server */
341
        private function acceptStream($server): void
342
        {
343
                $socket = @stream_socket_accept($server, 0); // @ a false alarm is harmless
×
344
                if ($socket !== false) {
×
345
                        $this->acceptConnection($socket);
×
346
                }
347
        }
×
348

349

350
        /**
351
         * Registers the connection, starts the workers that waited for it, and hands it a path. It is read without
352
         * blocking: a worker may have sent only a part of its line, and waiting for the rest would stop the parent from
353
         * hearing the others and from noticing a worker that is stuck.
354
         * @param resource $socket
355
         */
356
        private function acceptConnection($socket): void
357
        {
358
                stream_set_blocking($socket, enable: false);
1 ✔
359
                $this->accepted++;
1 ✔
360
                for (; $this->pending > 0; $this->pending--) {
1 ✔
361
                        $this->workers[] = $this->spawn($this->address);
1 ✔
362
                }
363

364
                $id = (int) $socket;
1 ✔
365
                $this->sockets[$id] = $socket;
1 ✔
366
                $this->assign($id);
1 ✔
367
        }
1 ✔
368

369

370
        private function closeServer(): void
371
        {
372
                if ($this->server instanceof \Socket) {
1 ✔
373
                        socket_close($this->server);
1 ✔
374
                } elseif ($this->server !== null) {
1 ✔
375
                        fclose($this->server);
×
376
                }
377

378
                $this->server = null;
1 ✔
379
        }
1 ✔
380

381

382
        /**
383
         * The results the worker has finished sending, by path: every line answers the path in progress, and a line
384
         * about any other one is a failure, as is an ended worker with a path in progress.
385
         * @return array<string, FileResult>
386
         */
387
        private function receive(int $id): array
388
        {
389
                $socket = $this->sockets[$id] ?? null;
1 ✔
390
                if ($socket === null) {
1 ✔
391
                        return [];
×
392
                }
393

394
                $chunk = fread($socket, 1 << 20);
1 ✔
395
                if ($chunk === false || ($chunk === '' && feof($socket))) {
1 ✔
396
                        if (isset($this->inProgress[$id])) {
1 ✔
397
                                $end = $this->findWorkerFailure();
1 ✔
398
                                throw new \RuntimeException('A worker ended ' . ($end === null ? '' : "$end ") . "while processing `{$this->inProgress[$id][0]}`." . $this->collectErrors());
1 ✔
399
                        }
400

401
                        $this->disconnect($id);
×
402
                        return [];
×
403
                }
404

405
                $buffer = ($this->buffers[$id] ?? '') . $chunk;
1 ✔
406
                $results = [];
1 ✔
407
                while (($end = strpos($buffer, "\n")) !== false) {
1 ✔
408
                        $data = json_decode(substr($buffer, 0, $end), associative: true);
1 ✔
409
                        $buffer = substr($buffer, $end + 1);
1 ✔
410
                        [$path, , $code] = $this->inProgress[$id] ?? [null, null, ''];
1 ✔
411
                        if (!is_array($data) || $path === null || ($data['path'] ?? null) !== base64_encode($path)) {
1 ✔
412
                                throw new \RuntimeException('A worker sent an unexpected message.' . $this->collectErrors());
1 ✔
413
                        }
414

415
                        unset($this->inProgress[$id]);
1 ✔
416
                        foreach (is_array($data['dependencies'] ?? null) ? $data['dependencies'] : [] as $file) {
1 ✔
417
                                $this->dependencies[(string) $file] = true;
1 ✔
418
                        }
419

420
                        foreach (is_array($data['runWarnings'] ?? null) ? $data['runWarnings'] : [] as $warning) {
1 ✔
421
                                $this->warnings[(string) $warning] = true;
1 ✔
422
                        }
423

424
                        $results[$path] = WorkerCodec::decode($data, $code);
1 ✔
425
                        $this->assign($id);
1 ✔
426
                }
427

428
                if (isset($this->sockets[$id]) && strlen($buffer)) {
1 ✔
429
                        $this->buffers[$id] = $buffer;
1 ✔
430
                } else {
431
                        unset($this->buffers[$id]);
1 ✔
432
                }
433

434
                return $results;
1 ✔
435
        }
436

437

438
        /** Hands the next path to the worker, or closes its connection when there is none. */
439
        private function assign(int $id): void
440
        {
441
                $path = array_shift($this->queue);
1 ✔
442
                if ($path === null) {
1 ✔
443
                        $this->disconnect($id);
1 ✔
444
                        return;
1 ✔
445
                }
446

447
                $read = $this->read ?? throw new \LogicException('No run is in progress.');
1 ✔
448
                $this->inProgress[$id] = [$path, microtime(as_float: true), $read($path)];
1 ✔
449
                fwrite($this->sockets[$id], json_encode(['path' => base64_encode($path)], JSON_THROW_ON_ERROR) . "\n"); // a path need not be UTF-8
1 ✔
450
        }
1 ✔
451

452

453
        /** @return array<string, float>  path in progress => the time it started */
454
        private function getInProgress(): array
455
        {
456
                $paths = [];
×
457
                foreach ($this->inProgress as [$path, $since]) {
×
458
                        $paths[$path] = $since;
×
459
                }
460

461
                return $paths;
×
462
        }
463

464

465
        private function disconnect(int $id): void
466
        {
467
                fclose($this->sockets[$id]);
1 ✔
468
                unset($this->sockets[$id], $this->inProgress[$id], $this->buffers[$id]);
1 ✔
469
        }
1 ✔
470

471

472
        /** @return array{resource, string}  process, file with its output */
473
        private function spawn(string $address): array
474
        {
475
                $output = (string) tempnam(sys_get_temp_dir(), 'dresscode-worker-');
1 ✔
476
                $process = @proc_open( // @ is escalated to exception
1 ✔
477
                        [...$this->command, '--worker', $address],
1 ✔
478
                        [0 => ['pipe', 'r'], 1 => ['file', $output, 'a'], 2 => ['file', $output, 'a']],
1 ✔
479
                        $pipes,
480
                        $this->cwd,
1 ✔
481
                );
482
                if ($process === false) {
1 ✔
483
                        throw new \RuntimeException('Cannot start a worker process: `' . implode(' ', $this->command) . '`');
×
484
                }
485

486
                fclose($pipes[0]);
1 ✔
487
                $this->spawnedAt = microtime(as_float: true);
1 ✔
488
                return [$process, $output];
1 ✔
489
        }
490

491

492
        /**
493
         * A worker that exited with an error, one that has spent longer than the timeout on a file, a worker the run waits
494
         * for that has not connected within its timeout, or every worker gone while results are missing fails the run.
495
         */
496
        private function checkWorkers(bool $resultsMissing): void
497
        {
498
                $running = false;
1 ✔
499
                foreach ($this->workers as [$process]) {
1 ✔
500
                        $status = proc_get_status($process);
1 ✔
501
                        if ($status['running']) {
1 ✔
502
                                $running = true;
1 ✔
503
                        } elseif ($status['exitcode'] !== 0) {
1 ✔
504
                                throw new \RuntimeException('A worker ended ' . self::formatEnd($status) . '.' . $this->collectErrors());
1 ✔
505
                        }
506
                }
507

508
                foreach ($this->inProgress as [$path, $since]) {
1 ✔
509
                        if (microtime(as_float: true) - $since > $this->taskTimeout) {
1 ✔
510
                                throw new \RuntimeException("A worker has spent more than $this->taskTimeout s on `$path`; a rule has most likely looped." . $this->collectErrors());
1 ✔
511
                        }
512
                }
513

514
                if (
515
                        $this->sockets === []
1 ✔
516
                        && $this->accepted < count($this->workers)
1 ✔
517
                        && microtime(as_float: true) - $this->spawnedAt > $this->connectTimeout
1 ✔
518
                ) {
519
                        throw new \RuntimeException("A worker has not connected within $this->connectTimeout s." . $this->collectErrors());
1 ✔
520
                }
521

522
                if (!$running && $resultsMissing) {
1 ✔
523
                        throw new \RuntimeException('The workers ended before processing every file.' . $this->collectErrors());
×
524
                }
525
        }
1 ✔
526

527

528
        /**
529
         * How a worker that failed ended, given a moment to end, because its connection closes before its process is gone;
530
         * null where none has failed.
531
         */
532
        private function findWorkerFailure(): ?string
533
        {
534
                $deadline = microtime(as_float: true) + self::EndTimeout;
1 ✔
535
                do {
536
                        foreach ($this->workers as [$process]) {
1 ✔
537
                                $status = proc_get_status($process);
1 ✔
538
                                if (!$status['running'] && $status['exitcode'] !== 0) {
1 ✔
539
                                        return self::formatEnd($status);
1 ✔
540
                                }
541
                        }
542

543
                        usleep(10_000);
1 ✔
544
                } while (microtime(as_float: true) < $deadline);
1 ✔
545

546
                return null;
×
547
        }
548

549

550
        /**
551
         * How a process ended, for a message: its exit code, one of Windows in the form it is known by (`0xC0000005` for a
552
         * crash of PHP itself), or the signal that ended it.
553
         * @param  array{signaled: bool, termsig: int, exitcode: int}  $status
554
         */
555
        private static function formatEnd(array $status): string
556
        {
557
                $code = $status['exitcode'];
1 ✔
558
                return match (true) {
559
                        $status['signaled'] => "on signal {$status['termsig']}",
1 ✔
560
                        PHP_OS_FAMILY === 'Windows' && ($code < 0 || $code > 0xFFFF) => sprintf('with code 0x%08X', $code & 0xFFFFFFFF),
1 ✔
561
                        default => "with code $code",
1 ✔
562
                };
563
        }
564

565

566
        private function collectErrors(): string
567
        {
568
                $errors = '';
1 ✔
569
                foreach ($this->workers as [, $output]) {
1 ✔
570
                        $errors .= trim((string) @file_get_contents($output)); // @ file may have been deleted meanwhile
1 ✔
571
                }
572

573
                return $errors === '' ? '' : "\n$errors";
1 ✔
574
        }
575

576

577
        private function stop(): void
578
        {
579
                foreach ($this->workers as [$process, $output]) {
1 ✔
580
                        if (proc_get_status($process)['running']) {
1 ✔
581
                                proc_terminate($process);
1 ✔
582
                        }
583

584
                        proc_close($process);
1 ✔
585
                        @unlink($output); // @ file may have been deleted meanwhile
1 ✔
586
                }
587

588
                $this->workers = $this->sockets = $this->inProgress = $this->buffers = [];
1 ✔
589
                $this->accepted = $this->pending = 0;
1 ✔
590
                $this->read = null;
1 ✔
591
        }
1 ✔
592
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc