Skip to content

Commit

Permalink
Enabled StyleCI (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell authored May 22, 2021
1 parent b1a505d commit e0566f9
Show file tree
Hide file tree
Showing 69 changed files with 870 additions and 832 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.styleci.yml export-ignore
CHANGELOG.md export-ignore
phpcs.xml.dist export-ignore
phpstan.neon.dist export-ignore
phpunit.xml.dist export-ignore
README.md export-ignore
Expand Down
32 changes: 0 additions & 32 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,6 @@ on:
pull_request:

jobs:
phpcs:
name: PHP CodeSniffer
runs-on: ubuntu-20.04

steps:
- name: Checkout Code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: composer:v2
coverage: none

- name: Install Dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --no-interaction --no-progress

- name: Install PHP CodeSniffer
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer bin phpcs update --no-interaction --no-progress

- name: Execute PHP CodeSniffer
run: vendor/bin/phpcs

phpstan:
name: PHPStan
runs-on: ubuntu-20.04
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.phpunit.result.cache
composer.lock
phpcs.xml
phpstan.neon
phpunit.xml
vendor
20 changes: 20 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
preset: symfony

risky: true

enabled:
- align_phpdoc
- alpha_ordered_imports
- array_indentation
- const_visibility_required
- native_constant_invocation
- native_function_invocation
- phpdoc_order
- void_return

disabled:
- native_constant_invocation_symfony
- native_function_invocation_symfony
- no_superfluous_phpdoc_tags_symfony
- phpdoc_to_comment
- phpdoc_var_without_name
6 changes: 3 additions & 3 deletions examples/form-submit.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require __DIR__ . '/../vendor/autoload.php';
require __DIR__.'/../vendor/autoload.php';

use HeadlessChromium\BrowserFactory;

Expand All @@ -10,7 +10,7 @@

// navigate to a page with a form
$page = $browser->createPage();
$page->navigate('file://' . __DIR__ . '/html/form.html')->waitForNavigation();
$page->navigate('file://'.__DIR__.'/html/form.html')->waitForNavigation();

// put 'hello' in the input and submit the form
$evaluation = $page->evaluate(
Expand All @@ -26,4 +26,4 @@
// get value in the new page
$value = $page->evaluate('document.querySelector("#value").innerHTML')->getReturnValue();

var_dump($value);
\var_dump($value);
14 changes: 7 additions & 7 deletions examples/persistent-browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* of creating a new one. If chrome was closed or crashed, a new instance is started again.
*/

require(__DIR__ . '/../vendor/autoload.php');
require __DIR__.'/../vendor/autoload.php';

// path to the file to store websocket's uri
$socketFile = '/tmp/chrome-php-demo-socket';
Expand All @@ -19,12 +19,12 @@
$browser = null;

// try to connect to chrome instance if it exists
if (file_exists($socketFile)) {
$socket = file_get_contents($socketFile);
if (\file_exists($socketFile)) {
$socket = \file_get_contents($socketFile);

try {
$browser = \HeadlessChromium\BrowserFactory::connectToBrowser($socket, [
'debugLogger' => 'php://stdout'
'debugLogger' => 'php://stdout',
]);
} catch (\HeadlessChromium\Exception\BrowserConnectionFailed $e) {
// The browser was probably closed
Expand All @@ -37,14 +37,14 @@
$factory = new \HeadlessChromium\BrowserFactory();
$browser = $factory->createBrowser([
'headless' => false,
'keepAlive' => true
'keepAlive' => true,
]);

// save the uri to be able to connect again to browser
file_put_contents($socketFile, $browser->getSocketUri());
\file_put_contents($socketFile, $browser->getSocketUri());
}

// do something with the browser
$page = $browser->createPage();

$page->navigate('http://example.com')->waitForNavigation();
$page->navigate('http://example.com')->waitForNavigation();
36 changes: 0 additions & 36 deletions phpcs.xml.dist

This file was deleted.

8 changes: 4 additions & 4 deletions src/AutoDiscover.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AutoDiscover
{
public function getChromeBinaryPath(): string
{
if (array_key_exists('CHROME_PATH', $_SERVER)) {
if (\array_key_exists('CHROME_PATH', $_SERVER)) {
return $_SERVER['CHROME_PATH'];
}

Expand All @@ -37,11 +37,11 @@ private static function windows(): string
{
try {
// accessing the registry can be costly, but this specific key is likely to be already cached in memory
$registryKey = shell_exec(
$registryKey = \shell_exec(
'reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe" /ve'
);

preg_match('/.:(?!.*:).*/', $registryKey, $matches);
\preg_match('/.:(?!.*:).*/', $registryKey, $matches);

return $matches[0];
} catch (\Throwable $e) {
Expand All @@ -52,6 +52,6 @@ private static function windows(): string

public function getOS(): string
{
return PHP_OS;
return \PHP_OS;
}
}
38 changes: 20 additions & 18 deletions src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use HeadlessChromium\Communication\Message;
use HeadlessChromium\Communication\Target;
use HeadlessChromium\Exception\CommunicationException;
use HeadlessChromium\Exception\NoResponseAvailable;
use HeadlessChromium\Exception\CommunicationException\ResponseHasError;
use HeadlessChromium\Exception\NoResponseAvailable;
use HeadlessChromium\Exception\OperationTimedOut;

class Browser
Expand All @@ -32,7 +32,8 @@ class Browser
protected $targets = [];

/**
* A preScript to be automatically added on every new pages
* A preScript to be automatically added on every new pages.
*
* @var string|null
*/
protected $pagePreScript;
Expand All @@ -42,15 +43,13 @@ public function __construct(Connection $connection)
$this->connection = $connection;

// listen for target created
$this->connection->on(Connection::EVENT_TARGET_CREATED, function (array $params) {

$this->connection->on(Connection::EVENT_TARGET_CREATED, function (array $params): void {
// create and store the target
$this->targets[$params['targetInfo']['targetId']] = new Target($params['targetInfo'], $this->connection);
});

// listen for target info changed
$this->connection->on(Connection::EVENT_TARGET_INFO_CHANGED, function (array $params) {

$this->connection->on(Connection::EVENT_TARGET_INFO_CHANGED, function (array $params): void {
// get target by id
$target = $this->getTarget($params['targetInfo']['targetId']);

Expand All @@ -60,8 +59,7 @@ public function __construct(Connection $connection)
});

// listen for target destroyed
$this->connection->on(Connection::EVENT_TARGET_DESTROYED, function (array $params) {

$this->connection->on(Connection::EVENT_TARGET_DESTROYED, function (array $params): void {
// get target by id
$target = $this->getTarget($params['targetId']);

Expand All @@ -71,7 +69,7 @@ public function __construct(Connection $connection)
$target->destroy();
$this->connection
->getLogger()
->debug('✘ target(' . $params['targetId'] . ') was destroyed and unreferenced.');
->debug('✘ target('.$params['targetId'].') was destroyed and unreferenced.');
}
});

Expand All @@ -93,26 +91,27 @@ public function getConnection(): Connection
*
* @param string|null $script
*/
public function setPagePreScript(string $script = null)
public function setPagePreScript(string $script = null): void
{
$this->pagePreScript = $script;
}

/**
* Closes the browser
* Closes the browser.
*
* @throws \Exception
*/
public function close()
public function close(): void
{
$this->sendCloseMessage();
}

/**
* Send close message to the browser
* Send close message to the browser.
*
* @throws OperationTimedOut
*/
final public function sendCloseMessage()
final public function sendCloseMessage(): void
{
$r = $this->connection->sendMessageSync(new Message('Browser.close'));
if (!$r->isSuccessful()) {
Expand All @@ -123,15 +122,16 @@ final public function sendCloseMessage()
}

/**
* Creates a new page
* Creates a new page.
*
* @throws NoResponseAvailable
* @throws CommunicationException
* @throws OperationTimedOut
*
* @return Page
*/
public function createPage(): Page
{

// page url
$params = ['url' => 'about:blank'];

Expand Down Expand Up @@ -179,14 +179,16 @@ public function createPage(): Page

/**
* @param string $targetId
*
* @return Target|null
*/
public function getTarget($targetId)
{
// make sure target was created (via Target.targetCreated event)
if (!array_key_exists($targetId, $this->targets)) {
if (!\array_key_exists($targetId, $this->targets)) {
return null;
}

return $this->targets[$targetId];
}

Expand All @@ -195,6 +197,6 @@ public function getTarget($targetId)
*/
public function getTargets()
{
return array_values($this->targets);
return \array_values($this->targets);
}
}
Loading

0 comments on commit e0566f9

Please sign in to comment.