当前位置: 首页 > 知识库问答 >
问题:

phpdoc:PHP警告:count():参数必须是实现可计数的数组或对象

欧阳学真
2023-03-14

安装

$ wget http://www.phpdoc.org/phpDocumentor.phar
$ chmod +x phpdocumentor.phar

让我们试着上一节课

$ cat src/Classe.php
<?php

/**
 * Classe.
 */
class Classe {}

$ ./phpdocumentor.phar -f src/Classe.php
Collecting files .. OK
Initializing parser .. OK
Parsing files
blablabla...

好吧但是现在让我们尝试一个函数

$ cat src/fun.php
<?php
/**
 * Summary fun.php
 */

/**
 * Function.
 */
function fun() {}

$ ./phpdocumentor.phar -f src/fun.php
Collecting files .. OK
Initializing parser .. OK
Parsing files
blablabla...
PHP Warning:  count(): Parameter must be an array or an object that implements Countable in phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293
PHP Warning:  count(): Parameter must be an array or an object that implements Countable in phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293
  Execute transformation using writer "twig"
PHP Warning:  count(): Parameter must be an array or an object that implements Countable in phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293
PHP Warning:  count(): Parameter must be an array or an object that implements Countable in phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
  Execute transformation using writer "twig"
PHP Warning:  count(): Parameter must be an array or an object that implements Countable in phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293
PHP Warning:  count(): Parameter must be an array or an object that implements Countable in phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293
blablabla...

现在有错误(警告)。为什么啊?

我还尝试通过composer安装phpdoc

$ composer require --dev phpDocumentor/phpDocumentor
blablabla...
Package kherge/version is abandoned, you should avoid using it. No replacement was suggested.
Package herrera-io/json is abandoned, you should avoid using it. Use kherge/json instead.
Package herrera-io/phar-update is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating autoload files

但结果更糟

$ vendor/bin/phpdoc -f src/Classe.php 
PHP Fatal error:  Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "@JMS\Serializer\Annotation\Type" in property phpDocumentor\Configuration::$title does not exist, or could not be auto-loaded. in vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php:54
Stack trace:
#0 vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(741): Doctrine\Common\Annotations\AnnotationException::semanticalError('The annotation ...')
#1 vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(663): Doctrine\Common\Annotations\DocParser->Annotation()
#2 vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php(354): Doctrine\Common\Annotations\DocParser->Annotations()
#3 vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php(254): Doctrine\Common\Annotations\Doc in vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 54

在这一点上,我想知道,这个phpdoc是一个有效的工具吗?是我不能用的吗?发生了什么?

共有2个答案

易风华
2023-03-14
++ composer require --dev phpdocumentor/phpdocumentor dev-master
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files

++ php -v
PHP 7.2.19-0ubuntu0.19.04.2 (cli) (built: Aug 13 2019 11:45:23) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.19-0ubuntu0.19.04.2, Copyright (c) 1999-2018, by Zend Technologies

++ cat src/Assembler.php
<?php
/**
 * File summary.
 */

namespace Xyz;

/**
 * @package Abc
 */
class Assembler {

    function assemble(?string $string) {

    }

}

++ php vendor/bin/phpdoc -f src/Assembler.php --template=responsive
Collecting files .. OK
Initializing parser .. OK
Parsing files
Parsing /mnt/shared-ntfs/Downloads/test-phpdoc/src/Assembler.php
Parse Error: Syntax error, unexpected '?', expecting T_VARIABLE on line 13  No summary was found for this file
Storing cache in "/mnt/shared-ntfs/Downloads/test-phpdoc/output/build" .. OK
Load cache                                                         ..    0.001s
Preparing template "responsive"                                    ..    0.006s
Preparing 13 transformations                                       ..    0.000s
Build "elements" index                                             ..    0.000s
Replace textual FQCNs with object aliases                          ..    0.000s
Resolve @link and @see tags in descriptions                        ..    0.000s
Enriches inline example tags with their sources                    ..    0.000s
Build "packages" index                                             ..    0.001s
Build "namespaces" index and add namespaces to "elements"          ..    0.000s
Collect all markers embedded in tags                               ..    0.000s
Transform analyzed project into artifacts                          .. 
Applying 13 transformations
  Initialize writer "phpDocumentor\Plugin\Core\Transformer\Writer\FileIo"
  Initialize writer "phpDocumentor\Plugin\Core\Transformer\Writer\Xml"
  Initialize writer "phpDocumentor\Plugin\Core\Transformer\Writer\Xsl"
  Initialize writer "phpDocumentor\Plugin\Graphs\Writer\Graph"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "FileIo"
  Execute transformation using writer "xml"
  Execute transformation using writer "xsl"
  Execute transformation using writer "xsl"
  Execute transformation using writer "xsl"
  Execute transformation using writer "xsl"
  Execute transformation using writer "xsl"
  Execute transformation using writer "xsl"
  Execute transformation using writer "xsl"
  Execute transformation using writer "xsl"
  Execute transformation using writer "Graph"
   0.133s
Analyze results and write report to log                            ..    0.000s

分析错误:语法错误,意外的“?”,第13行应为T_变量。未找到此文件的摘要

有没有可能PHP 7.2和phpdoc不能真正同意?是否有任何phpdoc版本与php 7.2兼容?

杨起运
2023-03-14

发生这些错误的原因是,您正在使用非数组的参数调用count方法,或者没有实现Countable接口。在PHP7.2之前,如果调用count(null)将返回值0,并且不会发出警告,在PHP7.2之后(包括PHP7.2),当使用无效参数调用count时会发出警告。正如您可以在此处检查的那样,第一个参数必须是:

数组或可数对象。

这意味着phpdocumentor.phar有一些代码与PHP7.2不完全兼容。您可以尝试降级php版本或升级phpdocumentar(如果可能)。

 类似资料: