Skip to content

Commit

Permalink
Merge pull request #63 from Icinga/phpstan
Browse files Browse the repository at this point in the history
Github Actions: Add PhpStan
  • Loading branch information
nilmerg authored Aug 30, 2023
2 parents b836b1c + 1ad83b9 commit 211518d
Show file tree
Hide file tree
Showing 7 changed files with 391 additions and 36 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['7.2', '7.3', '7.4', '8.0', '8.1']
php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
os: ['ubuntu-latest']

steps:
Expand All @@ -30,21 +30,21 @@ jobs:
php-version: ${{ matrix.php }}
tools: phpcs

# The cleanup is required because otherwise the tests fail on PHP 8+.
# Some of our deps are not officially compatible with that but will
# pass the lint tests. Removing composer.json and composer.lock first
# ensures that the test dep setup succeeds. Not removing vendor, but
# only renaming it ensures the files are scanned still.
- name: Cleanup
run: rm composer.* && mv vendor real-vendor

- name: Setup dependencies
run: composer require -n --no-progress overtrue/phplint
run: |
composer require -n --no-progress overtrue/phplint
git clone --depth 1 https://github.com/Icinga/icingaweb2.git vendor/icingaweb2
git clone --depth 1 -b snapshot/nightly https://github.com/Icinga/icinga-php-library.git vendor/icinga-php-library
git clone --depth 1 -b snapshot/nightly https://github.com/Icinga/icinga-php-thirdparty.git vendor/icinga-php-thirdparty
- name: PHP Lint
if: success() || matrix.allow_failure
run: ./vendor/bin/phplint -n --exclude={^vendor/.*} -- .

- name: PHP CodeSniffer
if: success() || matrix.allow_failure
if: ${{ ! cancelled() }}
run: phpcs

- name: PHPStan
if: ${{ ! cancelled() }}
uses: php-actions/phpstan@v3
21 changes: 13 additions & 8 deletions library/Pdfexport/HeadlessChrome.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,11 @@ public function toPdf()
try {
$result = $this->jsonVersion($this->remote[0], $this->remote[1]);
$parts = explode('/', $result['webSocketDebuggerUrl']);
$pdf = $this->printToPDF(join(':', $this->remote), end($parts), isset($this->document)
? $this->document->getPrintParameters()
: []);
$pdf = $this->printToPDF(
join(':', $this->remote),
end($parts),
! $this->document->isEmpty() ? $this->document->getPrintParameters() : []
);
break;
} catch (Exception $e) {
if ($this->binary === null) {
Expand Down Expand Up @@ -315,9 +317,11 @@ public function toPdf()
$loop->cancelTimer($killer);

try {
$pdf = $this->printToPDF($matches[1], $matches[2], isset($this->document)
? $this->document->getPrintParameters()
: []);
$pdf = $this->printToPDF(
$matches[1],
$matches[2],
! $this->document->isEmpty() ? $this->document->getPrintParameters() : []
);
} catch (Exception $e) {
Logger::error('Failed to print PDF. An error occurred: %s', $e);
}
Expand Down Expand Up @@ -403,7 +407,7 @@ private function printToPDF($socket, $browserId, array $parameters)

// wait for page to fully load
$this->waitFor($page, 'Page.frameStoppedLoading', ['frameId' => $frameId]);
} elseif (isset($this->document)) {
} elseif (! $this->document->isEmpty()) {
// If there's no url to load transfer the document's content directly
$this->communicate($page, 'Page.setDocumentContent', [
'frameId' => $targetId,
Expand All @@ -420,7 +424,7 @@ private function printToPDF($socket, $browserId, array $parameters)
$this->waitFor($page, self::WAIT_FOR_NETWORK);

// Wait for layout to initialize
if (isset($this->document)) {
if (! $this->document->isEmpty()) {
// Ensure layout scripts work in the same environment as the pdf printing itself
$this->communicate($page, 'Emulation.setEmulatedMedia', ['media' => 'print']);

Expand Down Expand Up @@ -588,6 +592,7 @@ private function waitFor(Client $ws, $eventName, array $expectedParams = null)
$wait = true;
$interceptedPos = -1;

$params = null;
do {
if (isset($this->interceptedEvents[++$interceptedPos])) {
$response = $this->interceptedEvents[$interceptedPos];
Expand Down
26 changes: 13 additions & 13 deletions library/Pdfexport/PrintableHtmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to false.
*
* @var bool
* @var ?bool
*/
protected $landscape;

Expand All @@ -71,7 +71,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to false.
*
* @var bool
* @var ?bool
*/
protected $printBackground;

Expand All @@ -80,7 +80,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to 1.
*
* @var float
* @var ?float
*/
protected $scale;

Expand All @@ -89,7 +89,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to 8.5 inches.
*
* @var float
* @var ?float
*/
protected $paperWidth;

Expand All @@ -98,7 +98,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to 11 inches.
*
* @var float
* @var ?float
*/
protected $paperHeight;

Expand All @@ -107,7 +107,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
* @var ?float
*/
protected $marginTop;

Expand All @@ -116,7 +116,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
* @var ?float
*/
protected $marginBottom;

Expand All @@ -125,7 +125,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
* @var ?float
*/
protected $marginLeft;

Expand All @@ -134,7 +134,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to 1cm (~0.4 inches).
*
* @var float
* @var ?float
*/
protected $marginRight;

Expand All @@ -143,7 +143,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to the empty string, which means print all pages
*
* @var string
* @var ?string
*/
protected $pageRanges;

Expand Down Expand Up @@ -174,7 +174,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
* With the default style, this height is separated by three lines, each accommodating 7px.
* Use `span`'s for single line text and `p`'s for multiline text.
*
* @var ValidHtml
* @var ?ValidHtml
*/
protected $headerTemplate;

Expand All @@ -194,7 +194,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
* With the default style, this height is separated by three lines, each accommodating 7px.
* Use `span`'s for single line text and `p`'s for multiline text.
*
* @var ValidHtml
* @var ?ValidHtml
*/
protected $footerTemplate;

Expand All @@ -210,7 +210,7 @@ class PrintableHtmlDocument extends BaseHtmlElement
*
* Defaults to false, in which case the content will be scaled to fit the paper size.
*
* @var bool
* @var ?bool
*/
protected $preferCSSPageSize;

Expand Down
5 changes: 4 additions & 1 deletion library/Pdfexport/ProvidedHook/Pdfexport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Icinga\Application\Hook;
use Icinga\Application\Hook\PdfexportHook;
use Icinga\Application\Icinga;
use Icinga\Application\Web;
use Icinga\Module\Pdfexport\HeadlessChrome;
use Icinga\Module\Pdfexport\PrintableHtmlDocument;
use iio\libmergepdf\Driver\TcpdiDriver;
Expand Down Expand Up @@ -123,7 +124,9 @@ public function streamPdfFromHtml($html, $filename)
$pdf = $merger->merge();
}

Icinga::app()->getResponse()
/** @var Web $app */
$app = Icinga::app();
$app->getResponse()
->setHeader('Content-Type', 'application/pdf', true)
->setHeader('Content-Disposition', "inline; filename=\"$filename\"", true)
->setBody($pdf)
Expand Down
6 changes: 3 additions & 3 deletions library/Pdfexport/ShellCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ShellCommand
/** @var int Exit code of the command */
protected $exitCode;

/** @var resource Process resource */
/** @var ?resource Process resource */
protected $resource;

/**
Expand Down Expand Up @@ -102,8 +102,8 @@ public function execute()
$stdout = '';
$stderr = '';

stream_set_blocking($namedpipes->stdout, 0); // non-blocking
stream_set_blocking($namedpipes->stderr, 0);
stream_set_blocking($namedpipes->stdout, false); // non-blocking
stream_set_blocking($namedpipes->stderr, false);

while (stream_select($read, $write, $except, 0, 20000) !== false) {
foreach ($read as $pipe) {
Expand Down
Loading

0 comments on commit 211518d

Please sign in to comment.