Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

count(): Parameter must be an array or an object that implements Countable #1914

Closed
thelounge-zz opened this issue Nov 20, 2017 · 23 comments
Closed

Comments

@thelounge-zz
Copy link

beside that it's time to support nullable return types of PHP 7.1 with PHP 7.2 you have tons of new runtime warnings:

Warning: count(): Parameter must be an array or an object that implements Countable in phar:///mnt/data/www/thelounge.net/contentlounge/updateservice/phpDocumentor.phar/src/phpDocumentor/Plugin/Core/Descriptor/Validator/Constraints/Functions/IsArgumentInDocBlockValidator.php on line 33

@thelounge-zz
Copy link
Author

[20-Nov-2017 15:16:28 Europe/Vienna] PHP Warning: count(): Parameter must be an array or an object that implements Countable in phar:///mnt/data/www/thelounge.net/contentlounge/updateservice/phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293

@jaapio jaapio added this to the 3.0.0 milestone Dec 24, 2017
@imelgrat
Copy link

I fixed the problem by checking whether $thing implements the Countable interface. Changed the function to:
twig_length_filter (using the @ fixed)
function twig_length_filter(Twig_Environment $env, $thing)
{
return is_scalar($thing) ? mb_strlen($thing, $env->getCharset()) : ($thing instanceof Countable ? count($thing):0);
}

@elb98rm
Copy link

elb98rm commented Mar 8, 2018

I can confirm the behaviour;

Warning: count(): Parameter must be an array or an object that implements Countable in phar://C:/wip/test-project/vendor/phpdocumentor/phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293 Warning: count(): Parameter must be an array or an object that implements Countable in phar://C:/wip/test-project/vendor/phpdocumentor/phpDocumentor.phar/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1293

Really affects the documentation.

@LewisJohnson
Copy link

To add to what @imelgrat said, you may want to change the file

IsArgumentInDocBlockValidator.php

You should change line 33 from

if ($value instanceof ValidationValueObject && count($value->argument) > 0)

to

if ($value instanceof ValidationValueObject && $value instanceof Countable && count($value->argument) > 0)

If you are using the PHAR version, you will have to extract it to a zip, then apply the fix

@thelounge-zz
Copy link
Author

frankly why can't there be a current devel-version as phar to download since even PHP 7.1.x syntax is not supported currently and so half of the documentations are not generated at all when the codebase is using nullable return types?

@jaapio
Copy link
Member

jaapio commented Mar 26, 2018

Please try the alpha version of v3. It is still work in progress but it will help you out.

@thelounge-zz
Copy link
Author

WHERE can i download a phar of v3

@thelounge-zz
Copy link
Author

don't work at all meaning generates empty docs

first it don't understand things like "--filename="/www/phpincludes/global*.php" any longer
Warning: file_get_contents(/www/phpincludes/global*.php): failed to open stream: No such file or directory in phar:///mnt/data/www/thelounge.net/contentlounge/updateservice/phpDocumentor.phar/vendor/phpdocumentor/reflection/src/phpDocumentor/Reflection/File/LocalFile.php on line 44

second the following call also leads in empty docs

/usr/bin/time -f "%E" php -d "extension=tokenizer.so" "$CMS_UPDATE_SERVICE_LOCATION/phpDocumentor.phar" run --force --progressbar --cache-folder="/tmp" --encoding="ISO-8859-1" --directory="$CMS_UPDATE_SERVICE_LOCATION/package" --filename="/www/phpincludes/newsletter.php" --ignore="modules/blog/tagcloud.php" --target="$CMS_UPDATE_SERVICE_LOCATION/documentation/code-referenzen/contentlounge" --title="ContentLounge - Technische Referenz" --defaultpackagename="ContentLounge" --template="clean"

ContentLounge - Technische Referenz
Collecting files .. OK
Initializing parser .. OK
Parsing files
1/1 [============================] 100%
Storing cache in "/tmp" .. OK
Load cache .. 0.001s
Preparing template "clean" .. 0.003s
Preparing 17 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.000s
Adding Parents to child interfaces .. 0.000s
Collect all markers embedded in tags .. 0.000s
Build "namespaces" index and add namespaces to "elements" .. 0.000s
Adding Parents to child classes .. 0.000s
17/17 [============================] 100% 0.055s ..
Analyze results and write report to log .. 0.000s
0:00.37

@wilroydmello
Copy link

I dont know , i did correct or not in php7.2

changed the function

function twig_length_filter(Twig_Environment $env, $thing)
{
    if (null === $thing) {
        return 0;
    }
    if (is_scalar($thing)) {
        return mb_strlen($thing, $env->getCharset());
    }
    if (is_object($thing) && method_exists($thing, '__toString') && !$thing instanceof \Countable) {
        return mb_strlen((string) $thing, $env->getCharset());
    }
    if ($thing instanceof \Countable || is_array($thing)) {
        return count($thing);
    }
    return 1;
}

get rid of warning but shows some message while doc generated in command prompt :

Unable to find the dot command of the GraphViz package. Is GraphViz correctly installed and present in your path?

@imelgrat
Copy link

@wilroydmello, the problem is not related to the twig_length_filter function itself but to not having GraphViz installed (or properly configured) on your machine. If you're running your site on Windows, the steps to resolve this error are:

  1. Download GraphViz Windows Packages from https://graphviz.gitlab.io/_pages/Download/Download_windows.html and run the installer
  2. Add c:\Program Files\Graphviz*\dot.exe or c:\Program Files (x86)\Graphviz*\dot.exe to your environment variable PATH
  3. Run phpdoc as usual.
  4. (Optional) If still shows the same error message, re-start your machine and run phpdoc again.

If you're running a Linux server, you need to run the following command to install and configure GrapViz

  • sudo apt-get install graphviz

After that, you should be able to run phpdoc using Graphviz

samwilson added a commit to wikimedia/eventmetrics that referenced this issue Aug 15, 2018
Doctrine\ORM\EntityRepository::findOneBy() returns NULL if nothing
is found, and as of PHP 7.2 this isn't countable.

Also add PHP 7.2 to CI, for future bug-catching, and
exclude phpdoc for now (pending resolution of
phpDocumentor/phpDocumentor#1914 ).
samwilson added a commit to wikimedia/eventmetrics that referenced this issue Aug 16, 2018
Doctrine\ORM\EntityRepository::findOneBy() returns NULL if nothing
is found, and as of PHP 7.2 this isn't countable.

Also add PHP 7.2 to CI, for future bug-catching, and
exclude phpdoc for now (pending resolution of
phpDocumentor/phpDocumentor#1914 ).
samwilson added a commit to wikimedia/eventmetrics that referenced this issue Aug 16, 2018
Doctrine\ORM\EntityRepository::findOneBy() returns NULL if nothing
is found, and as of PHP 7.2 this isn't countable.

Also add PHP 7.2 to CI, for future bug-catching, and
exclude phpdoc for now (pending resolution of
phpDocumentor/phpDocumentor#1914 ).
samwilson added a commit to wikimedia/eventmetrics that referenced this issue Aug 21, 2018
Switch to normal PHPUnit instead of the Symfony bridge, for
testing in CI. We'll return to the bridge after upgrading to
Symfony 4.

Doctrine\ORM\EntityRepository::findOneBy() returns NULL if nothing
is found, and as of PHP 7.2 this isn't countable.

Also add PHP 7.2 to CI, for future bug-catching, and
exclude phpdoc for now (pending resolution of
phpDocumentor/phpDocumentor#1914 ).
samwilson added a commit to wikimedia/eventmetrics that referenced this issue Aug 21, 2018
Switch to normal PHPUnit instead of the Symfony bridge, for
testing in CI. We'll return to the bridge after upgrading to
Symfony 4.

Doctrine\ORM\EntityRepository::findOneBy() returns NULL if nothing
is found, and as of PHP 7.2 this isn't countable.

Also add PHP 7.2 to CI, for future bug-catching, and
exclude phpdoc for now (pending resolution of
phpDocumentor/phpDocumentor#1914 ).
samwilson added a commit to wikimedia/eventmetrics that referenced this issue Aug 21, 2018
Switch to normal PHPUnit instead of the Symfony bridge, for
testing in CI. We'll return to the bridge after upgrading to
Symfony 4.

Doctrine\ORM\EntityRepository::findOneBy() returns NULL if nothing
is found, and as of PHP 7.2 this isn't countable.

Also add PHP 7.2 to CI, for future bug-catching, and
exclude phpdoc for now (pending resolution of
phpDocumentor/phpDocumentor#1914 ).
samwilson added a commit to wikimedia/eventmetrics that referenced this issue Aug 21, 2018
Switch to normal PHPUnit instead of the Symfony bridge, for
testing in CI. We'll return to the bridge after upgrading to
Symfony 4.

Doctrine\ORM\EntityRepository::findOneBy() returns NULL if nothing
is found, and as of PHP 7.2 this isn't countable.

Also add PHP 7.2 to CI, for future bug-catching, and
exclude phpdoc for now (pending resolution of
phpDocumentor/phpDocumentor#1914 ).
@Sominemo
Copy link

Applied both patches. Now it works OK on PHP 7.2 (phar file in zip archive)
phpdoc(7.2).phar.zip

rija pushed a commit to rija/gigadb-website that referenced this issue Sep 5, 2018
rija pushed a commit to rija/gigadb-website that referenced this issue Sep 5, 2018
@tflori
Copy link

tflori commented Sep 14, 2018

Please try the alpha version of v3. It is still work in progress but it will help you out.

I just want to give alpha.2 a try but I have no luck at all and I can't find any "breaking changes" document. It's so annoying - phpDocumentor.phar run -c phpdoc.xml... finished without errors but ignores the configuration file...

@LewisJohnson this is quiet useless patch:

if ($value instanceof ValidationValueObject && $value instanceof Countable && count($value->argument) > 0)

If $value is countable it doesn't mean $value->argument is also countable. A better solution:

$count = is_array($value->argument) || $value->argument instanceof \Countable ? count($value->argument) : 0;
if ($value instanceof ValidationValueObject && $count > 0)

davedevelopment added a commit to davedevelopment/mockery that referenced this issue Sep 27, 2018
See phpDocumentor/phpDocumentor#1914

Using the phar to avoid having to depend of dev versions of
phpDocumentor and it's dependencies in composer json
@FelipoAntonoff
Copy link

Applied both patches. Now it works OK on PHP 7.2 (phar file in zip archive)
phpdoc(7.2).phar.zip

Thank you very much, resolved here.
I use PHP 7.2 and with that phar generated by you, it has disappeared.

I saw this same problem on a 4 day open issue, it would be nice to update officially with the fix, thanks again for the help.

@Sominemo
Copy link

Sominemo commented Sep 29, 2018

I just should warn themes do not work in phar due to internal unfixable PHPDoc2 bug so if you want to use them you should use unpacked version.
php "path\to\phpdoc\bin\phpdoc" --config path\to\config\config.xml

#713

If you have ideas how to patch this just let me know

@FelipoAntonoff
Copy link

Thanks for the info.

@CaptainYarb
Copy link

The Dockerfile uses php:7 on Docker, which won't use 7.3 and is causing this issue occur on all current and develop releases on Docker hub.

@JakeQZ
Copy link

JakeQZ commented Jan 26, 2020

A better solution:

$count = is_array($value->argument) || $value->argument instanceof \Countable ? count($value->argument) : 0;
if ($value instanceof ValidationValueObject && $count > 0)

Actually, I think $value->argument should only be tested once $value instanceof ValidationValueObject is satisfied. And since $value->argument uses a magic __get it would be more efficient to store the result. Thus:

        if ($value instanceof ValidationValueObject) {
            $argument = $value->argument;
            if ((\is_array($argument) || $argument instanceof \Countable) && \count($argument) > 0) {
                /* @var $params \phpDocumentor\Descriptor\Collection */
                // ...
            }                
        }

@mvriel mvriel modified the milestones: 3.0.0, 2.9.1 Jan 27, 2020
@mvriel
Copy link
Member

mvriel commented Jan 27, 2020

This issue is about the Validators in phpDocumentor 2.9. That system was removed in 3.0 and is not scheduled to be reintroduced soon. phpDocumentor 2.9 is also not scheduled to include support for PHP 7 or higher.

As such, any issues in the validator mechanism should be resolved in the 2.9 branch and is not included in our work on 3.0.

To resolve this item soon, assistance is needed from the community in the form of a PR on the 2.9 branch.

@JakeQZ
Copy link

JakeQZ commented Jan 27, 2020

This has actually been fixed in the 2.9 branch by 3c10cdf (see #1808) but there has not been a 2.9.x release since then.

@mvriel
Copy link
Member

mvriel commented Jan 27, 2020

Hi @JakeQZ, I'm glad that you are paying more attention than I was 😅 We'll need to do a release then! At this moment I am wrapped in another effort; after that is finished I'll check out that release (2.9 did not have a fully automated release cycle yet)

@mvriel
Copy link
Member

mvriel commented Jan 27, 2020

Release is complete: https://github.com/phpDocumentor/phpDocumentor/releases/tag/v2.9.1

The phar is now downloadable from that location. I still need to build and release the PEAR archive but I am running into a couple issues there

@sjoller
Copy link

sjoller commented Jan 31, 2020

I downloaded the phar file from https://github.com/phpDocumentor/phpDocumentor/releases/tag/v2.9.1 and this gave me the exact same error, just this time it was twig acting up.

The twig_length_filter() method in twig/twig/lib/Twig/Extension/Core.php ends up returning a count on $thing, if everything else fails. I modified the method to only return a count on $thing, if it is a Countable object (otherwise just return 0), and that made phpDoc finish without further errors.

I have a suspicion, that it relates to how phpDoc uses twig, rather than a twig bug, but i only made it go away, by modifying the twig file, so no PR, unfortunately.

@jaapio
Copy link
Member

jaapio commented Jan 31, 2020

If you are using phpdocumentor in a modern project you might want to check our latest beta release. But please don't reply on closed issues. They are not traceable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Closed
Development

No branches or pull requests