Skip to content

Commit f35b22c

Browse files
committed
[+]: add simple auto-fix v2
1 parent a3ff666 commit f35b22c

File tree

6 files changed

+237
-13
lines changed

6 files changed

+237
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ git clone https://github.com/php/doc-en.git
88
git clone https://github.com/voku/php-doc-fixer.git
99
cd php-doc-fixer/
1010
composer update --prefer-dist
11-
php bin/phpdocfixer run [--auto-fix="true"] ../doc-en/reference
11+
php bin/phpdocfixer run [--auto-fix="true"] [--remove-array-value-info="true"] ../doc-en/reference
1212
```

src/voku/PhpDocFixer/CliCommand/PhpDocFixerCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public function configure(): void
3939
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
4040
'Automatically fix the types in the given xml files. (false or true)',
4141
'false'
42+
)
43+
->addOption(
44+
'remove-array-value-info',
45+
null,
46+
\Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL,
47+
'Automatically convert e.g. int[] into array. (false or true)',
48+
'false'
4249
);
4350
}
4451

@@ -50,6 +57,7 @@ public function execute(InputInterface $input, OutputInterface $output): int
5057
\assert(\is_string($realPath));
5158

5259
$autoFix = $input->getOption('auto-fix') !== 'false';
60+
$removeArrayValueInfo = $input->getOption('remove-array-value-info') !== 'false';
5361

5462
if (!$realPath || !\file_exists($realPath)) {
5563
$output->writeln('-------------------------------');
@@ -63,7 +71,10 @@ public function execute(InputInterface $input, OutputInterface $output): int
6371
$xmlDocInfo = $xmlReader->parse();
6472

6573
$phpStormStubsPath = __DIR__ . '/../../../../vendor/jetbrains/phpstorm-stubs/';
66-
$phpTypesFromPhpStormStubs = new \voku\PhpDocFixer\PhpStormStubs\PhpStormStubsReader($phpStormStubsPath);
74+
$phpTypesFromPhpStormStubs = new \voku\PhpDocFixer\PhpStormStubs\PhpStormStubsReader(
75+
$phpStormStubsPath,
76+
$removeArrayValueInfo
77+
);
6778
$phpStormStubsInfo = $phpTypesFromPhpStormStubs->parse();
6879

6980
$errors = [];

src/voku/PhpDocFixer/PhpStormStubs/PhpStormStubsReader.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ final class PhpStormStubsReader
88
{
99
private string $path;
1010

11-
public function __construct(string $path)
11+
private bool $removeArrayValueInfo;
12+
13+
public function __construct(string $path, bool $removeArrayValueInfo = false)
1214
{
1315
$this->path = $path;
16+
$this->removeArrayValueInfo = $removeArrayValueInfo;
1417
}
1518

1619
/**
@@ -26,8 +29,14 @@ public function parse(): array
2629
$functionInfo = $phpCode->getFunctionsInfo();
2730
foreach ($functionInfo as $functionName => $info) {
2831
$return[$functionName]['return'] = \ltrim($info['returnTypes']['typeFromPhpDocSimple'] ?? '', '\\');
32+
if ($this->removeArrayValueInfo) {
33+
$return[$functionName]['return'] = $this->removeArrayValueInfo($return[$functionName]['return']);
34+
}
2935
foreach ($info['paramsTypes'] as $paramName => $paramTypes) {
3036
$return[$functionName]['params'][$paramName] = \ltrim($paramTypes['typeFromPhpDocSimple'] ?? '', '\\');
37+
if ($this->removeArrayValueInfo) {
38+
$return[$functionName]['params'][$paramName] = $this->removeArrayValueInfo($return[$functionName]['params'][$paramName]);
39+
}
3140
}
3241
}
3342

@@ -36,12 +45,28 @@ public function parse(): array
3645
$className = (string) $class->name;
3746
foreach ($methodInfo as $methodName => $info) {
3847
$return[$className . '::' . $methodName]['return'] = \ltrim($info['returnTypes']['typeFromPhpDocSimple'] ?? '', '\\');
48+
if ($this->removeArrayValueInfo) {
49+
$return[$className . '::' . $methodName]['return'] = $this->removeArrayValueInfo($return[$className . '::' . $methodName]['return']);
50+
}
3951
foreach ($info['paramsTypes'] as $paramName => $paramTypes) {
4052
$return[$className . '::' . $methodName]['params'][$paramName] = \ltrim($paramTypes['typeFromPhpDocSimple'] ?? '', '\\');
53+
if ($this->removeArrayValueInfo) {
54+
$return[$className . '::' . $methodName]['params'][$paramName] = $this->removeArrayValueInfo($return[$className . '::' . $methodName]['params'][$paramName]);
55+
}
4156
}
4257
}
4358
}
4459

4560
return $return;
4661
}
62+
63+
/**
64+
* @param string $phpdoc_type
65+
*
66+
* @return string
67+
*/
68+
public function removeArrayValueInfo(string $phpdoc_type): string
69+
{
70+
return (string) \preg_replace('#([\w_]*\[\])#', 'array', $phpdoc_type);
71+
}
4772
}

src/voku/PhpDocFixer/XmlDocs/XmlReader.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ public function parse(): array
2828
$xmlParser->autoRemoveXPathNamespaces();
2929

3030
$finder = new \Symfony\Component\Finder\Finder();
31-
$finder->files()->in($this->xml_path);
31+
32+
if (\file_exists($this->xml_path) && !\is_dir($this->xml_path)) {
33+
$finder->files()->in(\dirname($this->xml_path))->name(\basename($this->xml_path));
34+
} else {
35+
$finder->files()->in($this->xml_path);
36+
}
3237

3338
$data = [[]];
3439
foreach ($finder as $file) {
@@ -40,12 +45,15 @@ public function parse(): array
4045
$absoluteFilePath = $file->getPath() . \DIRECTORY_SEPARATOR . $fileName;
4146
$content = $file->getContents();
4247

48+
$contentMethodKeyword = 'methodsynopsis';
4349
\preg_match('#<methodsynopsis>.*</methodsynopsis>#Uis', $content, $contentMethod);
4450
$contentMethod = $contentMethod[0] ?? null;
4551

52+
$contentMethodOopKeyword = 'methodsynopsis';
4653
\preg_match('#<methodsynopsis role="oop">.*</methodsynopsis>#Uis', $content, $contentMethodOop);
4754
$contentMethodOop = $contentMethodOop[0] ?? null;
4855

56+
$contentConstructorKeyword = 'constructorsynopsis';
4957
\preg_match('#<constructorsynopsis>.*</constructorsynopsis>#Uis', $content, $contentConstructor);
5058
$contentConstructor = $contentConstructor[0] ?? null;
5159

@@ -55,17 +63,17 @@ public function parse(): array
5563

5664
if ($contentMethod) {
5765
$methodsynopsis = $xmlParser->loadXml($contentMethod);
58-
$data[] = $this->findTypes($methodsynopsis, $absoluteFilePath);
66+
$data[] = $this->findTypes($methodsynopsis, $absoluteFilePath, $contentMethodKeyword);
5967
}
6068

6169
if ($contentMethodOop) {
6270
$methodsynopsisOop = $xmlParser->loadXml($contentMethodOop);
63-
$data[] = $this->findTypes($methodsynopsisOop, $absoluteFilePath);
71+
$data[] = $this->findTypes($methodsynopsisOop, $absoluteFilePath, $contentMethodOopKeyword);
6472
}
6573

6674
if ($contentConstructor) {
6775
$constructorsynopsis = $xmlParser->loadXml($contentConstructor);
68-
$data[] = $this->findTypes($constructorsynopsis, $absoluteFilePath);
76+
$data[] = $this->findTypes($constructorsynopsis, $absoluteFilePath, $contentConstructorKeyword);
6977
}
7078
}
7179

@@ -75,6 +83,7 @@ public function parse(): array
7583
/**
7684
* @param \voku\helper\XmlDomParser $xmlParser
7785
* @param string $absoluteFilePath
86+
* @param string $xmlWrapperElement
7887
*
7988
* @return array
8089
*
@@ -86,22 +95,23 @@ public function parse(): array
8695
*/
8796
private function findTypes(
8897
\voku\helper\XmlDomParser $xmlParser,
89-
string $absoluteFilePath
98+
string $absoluteFilePath,
99+
string $xmlWrapperElement
90100
): array {
91101
// init
92102
$data = [];
93103

94-
$name = $xmlParser->findOne('//methodname')->text();
104+
$name = $xmlParser->findOne('methodname')->text();
95105
$data[$name]['absoluteFilePath'] = $absoluteFilePath;
96106

97107
$returnTypesArray = [];
98-
$returnTypesTmp = $xmlParser->findMultiOrFalse('type.union type');
108+
$returnTypesTmp = $xmlParser->findMultiOrFalse($xmlWrapperElement . ' > type.union > type');
99109
if ($returnTypesTmp !== false) {
100110
foreach ($returnTypesTmp as $returnTypeTmp) {
101111
$returnTypeText = $returnTypeTmp->text();
102112
$returnTypesArray[$returnTypeText] = \ltrim($returnTypeText, '\\');
103113
}
104-
} elseif (($returnTypeTmp = $xmlParser->findOneOrFalse('type')) !== false) {
114+
} elseif (($returnTypeTmp = $xmlParser->findOneOrFalse($xmlWrapperElement . ' > type')) !== false) {
105115
$returnTypeText = $returnTypeTmp->text();
106116
$returnTypesArray[$returnTypeText] = \ltrim($returnTypeText, '\\');
107117
}
@@ -113,7 +123,7 @@ private function findTypes(
113123
$paramName = $param->findOne('parameter')->text();
114124
$paramTypesArray = [];
115125

116-
$paramTypesTmp = $param->findMultiOrFalse('type.union type');
126+
$paramTypesTmp = $param->findMultiOrFalse('type.union > type');
117127
if ($paramTypesTmp !== false) {
118128
foreach ($paramTypesTmp as $paramTypeTmp) {
119129
$paramTypeText = $paramTypeTmp->text();

tests/CheckerTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,37 @@ public static function testPhpStormStubsReader(): void
1515
$phpTypesFromPhpStormStubs = new \voku\PhpDocFixer\PhpStormStubs\PhpStormStubsReader($phpStormStubsPath);
1616
$phpStormStubsInfo = $phpTypesFromPhpStormStubs->parse();
1717

18-
static::assertSame('int|false', $phpStormStubsInfo['mb_strpos']['return']);
18+
$expected = [
19+
'return' => 'int|false',
20+
'params' => [
21+
'haystack' => 'string',
22+
'needle' => 'string',
23+
'offset' => 'int',
24+
'encoding' => 'string',
25+
],
26+
];
27+
28+
static::assertSame($expected, $phpStormStubsInfo['mb_strpos']);
29+
}
30+
31+
public static function testPhpDocXmlReader(): void
32+
{
33+
$xmlPath = __DIR__ . '/fixtures/bcpow.xml';
34+
$phpDocXmlReader = new \voku\PhpDocFixer\XmlDocs\XmlReader($xmlPath);
35+
$phpDocXmlReaderInfo = $phpDocXmlReader->parse();
36+
37+
$expected = [
38+
'bcpow' => [
39+
'absoluteFilePath' => '/home/lmoelleken/testing/git/php-doc-fixer/tests/fixtures/bcpow.xml',
40+
'return' => 'string',
41+
'params' => [
42+
'num' => 'string',
43+
'exponent' => 'string',
44+
'scale' => 'int|null',
45+
],
46+
],
47+
];
48+
49+
static::assertSame($expected, $phpDocXmlReaderInfo);
1950
}
2051
}

tests/fixtures/bcpow.xml

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- $Revision$ -->
3+
<refentry xml:id="function.bcpow" xmlns="http://docbook.org/ns/docbook">
4+
<refnamediv>
5+
<refname>bcpow</refname>
6+
<refpurpose>Raise an arbitrary precision number to another</refpurpose>
7+
</refnamediv>
8+
9+
<refsect1 role="description">
10+
&reftitle.description;
11+
<methodsynopsis>
12+
<type>string</type><methodname>bcpow</methodname>
13+
<methodparam><type>string</type><parameter>num</parameter></methodparam>
14+
<methodparam><type>string</type><parameter>exponent</parameter></methodparam>
15+
<methodparam choice="opt"><type class="union"><type>int</type><type>null</type></type><parameter>scale</parameter><initializer>&null;</initializer></methodparam>
16+
</methodsynopsis>
17+
<para>
18+
Raise <parameter>num</parameter> to the power
19+
<parameter>exponent</parameter>.
20+
</para>
21+
</refsect1>
22+
23+
<refsect1 role="parameters">
24+
&reftitle.parameters;
25+
<para>
26+
<variablelist>
27+
<varlistentry>
28+
<term><parameter>num</parameter></term>
29+
<listitem>
30+
<para>
31+
The base, as a string.
32+
</para>
33+
</listitem>
34+
</varlistentry>
35+
<varlistentry>
36+
<term><parameter>exponent</parameter></term>
37+
<listitem>
38+
<para>
39+
The exponent, as a string. If the exponent is non-integral, it is truncated.
40+
The valid range of the exponent is platform specific, but is at least
41+
<literal>-2147483648</literal> to <literal>2147483647</literal>.
42+
</para>
43+
</listitem>
44+
</varlistentry>
45+
&bc.scale.description;
46+
</variablelist>
47+
</para>
48+
</refsect1>
49+
50+
<refsect1 role="returnvalues">
51+
&reftitle.returnvalues;
52+
<para>
53+
Returns the result as a string.
54+
</para>
55+
</refsect1>
56+
57+
<refsect1 role="changelog"><!-- {{{ -->
58+
&reftitle.changelog;
59+
<informaltable>
60+
<tgroup cols="2">
61+
<thead>
62+
<row>
63+
<entry>&Version;</entry>
64+
<entry>&Description;</entry>
65+
</row>
66+
</thead>
67+
<tbody>
68+
<row>
69+
<entry>7.3.0</entry>
70+
<entry>
71+
<function>bcpow</function> now returns numbers with the requested scale.
72+
Formerly, the returned numbers may have omitted trailing decimal zeroes.
73+
</entry>
74+
</row>
75+
</tbody>
76+
</tgroup>
77+
</informaltable>
78+
</refsect1><!-- }}} -->
79+
80+
<refsect1 role="examples">
81+
&reftitle.examples;
82+
<example>
83+
<title><function>bcpow</function> example</title>
84+
<programlisting role="php">
85+
<![CDATA[
86+
<?php
87+
88+
echo bcpow('4.2', '3', 2); // 74.08
89+
90+
?>
91+
]]>
92+
</programlisting>
93+
</example>
94+
</refsect1>
95+
96+
<refsect1 role="notes">
97+
&reftitle.notes;
98+
<note>
99+
<para>
100+
Before PHP 7.3.0 <function>bcpow</function> may return a result with fewer digits after the
101+
decimal point than the <parameter>scale</parameter> parameter would
102+
indicate. This only occurs when the result doesn't require all of the
103+
precision allowed by the <parameter>scale</parameter>. For example:
104+
<example>
105+
<title><function>bcpow</function> scale example</title>
106+
<programlisting role="php">
107+
<![CDATA[
108+
<?php
109+
echo bcpow('5', '2', 2); // prints "25", not "25.00"
110+
?>
111+
]]>
112+
</programlisting>
113+
</example>
114+
</para>
115+
</note>
116+
</refsect1>
117+
118+
<refsect1 role="seealso">
119+
&reftitle.seealso;
120+
<para>
121+
<simplelist>
122+
<member><function>bcpowmod</function></member>
123+
<member><function>bcsqrt</function></member>
124+
</simplelist>
125+
</para>
126+
</refsect1>
127+
</refentry>
128+
<!-- Keep this comment at the end of the file
129+
Local variables:
130+
mode: sgml
131+
sgml-omittag:t
132+
sgml-shorttag:t
133+
sgml-minimize-attributes:nil
134+
sgml-always-quote-attributes:t
135+
sgml-indent-step:1
136+
sgml-indent-data:t
137+
indent-tabs-mode:nil
138+
sgml-parent-document:nil
139+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
140+
sgml-exposed-tags:nil
141+
sgml-local-catalogs:nil
142+
sgml-local-ecat-files:nil
143+
End:
144+
vim600: syn=xml fen fdm=syntax fdl=2 si
145+
vim: et tw=78 syn=sgml
146+
vi: ts=1 sw=1
147+
-->

0 commit comments

Comments
 (0)