简体   繁体   中英

phpdoc: PHP Warning: count(): Parameter must be an array or an object that implements Countable

Install

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

Let's try a class

$ cat src/Classe.php
<?php

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

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

All right. But now let's try a function

$ 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...

Now there are errors (Warning). Why?

I also tried installing phpdoc via composer

$ 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

But the result is even worse

$ 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

At this point I would like to know, is this phpdoc a valid tool? Am I the one who can't use it? What's wrong?

Those errors are happening because you are calling the count method with a parameter that is not an array or doesn't implement the Countable interface. Prior to PHP 7.2 if you called count(null) the value 0 would be returned and no warning would be issued, after (and including) PHP 7.2 that warning is issued when calling count with an invalid parameter. As you can check here the first parameter must be:

An array or Countable object.

This means that phpdocumentor.phar has some code that is not fully compatible with PHP 7.2+. You can try downgrading the php version or upgrading the phpdocumentar (if possible).

++ 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

Parse Error: Syntax error, unexpected '?', expecting T_VARIABLE on line 13 No summary was found for this file

Is it possible that php 7.2 and phpdoc can't really agree? Is there any phpdoc version compatible with php 7.2?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM