简体   繁体   中英

I got an error “Return value must be of the type int, array returned” when trying to count rows of imported excel file

I'm trying to count rows of imported excel file (first sheet only, I do it with DataTestImport.php), but I got an error: "Return value of App\\Imports\\DataTestImport::getRowCount1() must be of the type int, array returned". What should I do to solve that error?

DataTestController.php

$import = new DataTestImport;
print_r('Row count: ' . (int)implode($import->getRowCount1()));

DataTestImport.php

protected $import;

public function __construct()
{
    $this->import = new TestImport;
}

public function getRowCount1(): array
{
    return [
        0 => $this->import->getRowCount(),
    ];
}

TestImport.php

private $i = 0;

public function collection(Collection $rows)
{
    foreach ($rows as $row) {
        ** a lot of codes here **

        ++$this->i;
    }
}

public function getRowCount(): int
{
    return $this->i;
}

Just return int as you promise in function declaration

public function getRowCount1(): int
{
    return $this->import->getRowCount();
}

It's pretty straightforward, this function is clearly returning a PHP array, while the return type is an int ( public function... : int ):

public function getRowCount1(): int
{
    return [
        0 => $this->import->getRowCount(),
    ];
}

So you would probably want to change the type to an array instead:

public function getRowCount1(): array
{
    return [
        0 => $this->import->getRowCount(),
    ];
}

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