Skip to content

Commit

Permalink
Add support fo " use function" statements
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck committed Sep 24, 2020
1 parent 47c763b commit 87a137a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
6 changes: 6 additions & 0 deletions e2e/fixtures/php/foo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
// @OctoLinkerResolve(https://www.php.net/manual/en/class.arrayaccess.php)
use ArrayAccess;

// @OctoLinkerResolve(https://www.php.net/manual/en/function.is-array.php)
use function is_array;

// @OctoLinkerResolve(https://www.php.net/manual/en/function.preg-last-error.php)
use function preg_last_error;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\Container as ContainerContract;

Expand Down
8 changes: 8 additions & 0 deletions packages/helper-grammar-regex-collection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ export const PHP = regex`
${diffSigns}
use
\s
(?!function\s)
(?<$1>[^;\s]+)
`;

export const PHP_FUNC = regex`
${diffSigns}
use
\sfunction\s
(?<$1>[^;\s]+)
`;

Expand Down
13 changes: 12 additions & 1 deletion packages/helper-grammar-regex-collection/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,18 @@ const fixtures = {
['Illuminate\\Contracts\\Container\\BindingResolutionException'],
],
],
invalid: [''],
invalid: ['use function is_array'],
},
PHP_FUNC: {
valid: [
['use function is_array', ['is_array']],
['use function preg_last_error', ['preg_last_error']],
],
invalid: [
'use Closure',
'use Illuminate\\Contracts\\Container\\BindingResolutionException',
['Illuminate\\Contracts\\Container\\BindingResolutionException'],
],
},
NET_PROJ_SDK: {
valid: [['<Project Sdk="foo">', ['foo']]],
Expand Down
22 changes: 19 additions & 3 deletions packages/plugin-php/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { PHP_FUNC } from '@octolinker/helper-grammar-regex-collection';
import php from '../index';

describe('rust-crate', () => {
const path = '/blob/path/dummy';

it('resolves php using the live-resolver', () => {
expect(php.resolve(path, ['ArrayAccess'])).toEqual({
it('resolves classes', () => {
expect(php.resolve(path, ['FooBar'])).toEqual({
registry: 'ping',
target: 'https://www.php.net/manual/en/class.arrayaccess.php',
target: 'https://www.php.net/manual/en/class.foobar.php',
});
});

it('resolves functions', () => {
expect(php.resolve(path, ['Foo_Bar'], {}, PHP_FUNC)).toEqual({
registry: 'ping',
target: 'https://www.php.net/manual/en/function.foo-bar.php',
});
});

it('does not try to resolve packages', () => {
expect(
php.resolve(path, [
'Illuminate\\Contracts\\Container\\BindingResolutionException',
]),
).toEqual(undefined);
});
});
21 changes: 16 additions & 5 deletions packages/plugin-php/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { PHP } from '@octolinker/helper-grammar-regex-collection';
import { PHP, PHP_FUNC } from '@octolinker/helper-grammar-regex-collection';
import liveResolverQuery from '@octolinker/resolver-live-query';

export default {
name: 'PHP',

resolve(path, [target]) {
resolve(path, [target], meta, regExp) {
if (target.includes('\\')) {
return;
}

const docsUrl = `https://www.php.net/manual/en/class.${target.toLowerCase()}.php`;
return liveResolverQuery({ type: 'ping', target: docsUrl });
if (regExp === PHP_FUNC) {
return liveResolverQuery({
type: 'ping',
target: `https://www.php.net/manual/en/function.${target
.toLowerCase()
.replace(/_/g, '-')}.php`,
});
}

return liveResolverQuery({
type: 'ping',
target: `https://www.php.net/manual/en/class.${target.toLowerCase()}.php`,
});
},

getPattern() {
Expand All @@ -21,6 +32,6 @@ export default {
},

getLinkRegexes() {
return PHP;
return [PHP, PHP_FUNC];
},
};

0 comments on commit 87a137a

Please sign in to comment.