barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.12k stars 1.15k forks source link

Wrong methods on Collection when using maatwebsite/excel #1095

Open underdpt opened 3 years ago

underdpt commented 3 years ago

Versions:

Description:

I have installed package maatwebsite/excel version 3.1.24. That package makes use of custom collections, and I'm finding that laravel-ide-helper is using this package's DownloadCollection and StoreCollection as if it were Illuminate\Support\Collection. This is what I get on _ide_helper.php:

    namespace Illuminate\Support { 
            /**
     * 
     *
     */ 
        class Arr {

    }
            /**
     * 
     *
     */ 
        class Str {

    }
            /**
     * 
     *
     */ 
        class Collection {
                    /**
         * 
         *
         * @param string $fileName
         * @param string|null $writerType
         * @param mixed $withHeadings
         * @static 
         */ 
        public static function downloadExcel($fileName, $writerType = null, $withHeadings = false)
        {
                        return \Illuminate\Support\Collection::downloadExcel($fileName, $writerType, $withHeadings);
        }
                    /**
         * 
         *
         * @param string $filePath
         * @param string|null $disk
         * @param string|null $writerType
         * @param mixed $withHeadings
         * @static 
         */ 
        public static function storeExcel($filePath, $disk = null, $writerType = null, $withHeadings = false)
        {
                        return \Illuminate\Support\Collection::storeExcel($filePath, $disk, $writerType, $withHeadings);
        }

    }

}

This is vendor\maatwebsite\excel\src\Mixins\DownloadCollection.php:

<?php

namespace Maatwebsite\Excel\Mixins;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Sheet;

class DownloadCollection
{
    /**
     * @return callable
     */
    public function downloadExcel()
    {
        return function (string $fileName, string $writerType = null, $withHeadings = false) {
            $export = new class($this, $withHeadings) implements FromCollection, WithHeadings {
                use Exportable;

                /**
                 * @var bool
                 */
                private $withHeadings;

                /**
                 * @var Collection
                 */
                private $collection;

                /**
                 * @param Collection $collection
                 * @param bool       $withHeading
                 */
                public function __construct(Collection $collection, bool $withHeading = false)
                {
                    $this->collection   = $collection->toBase();
                    $this->withHeadings = $withHeading;
                }

                /**
                 * @return Collection
                 */
                public function collection()
                {
                    return $this->collection;
                }

                /**
                 * @return array
                 */
                public function headings(): array
                {
                    if (!$this->withHeadings) {
                        return [];
                    }

                    $firstRow = $this->collection->first();

                    if ($firstRow instanceof Arrayable || \is_object($firstRow)) {
                        return array_keys(Sheet::mapArraybleRow($firstRow));
                    }

                    return $this->collection->collapse()->keys()->all();
                }
            };

            return $export->download($fileName, $writerType);
        };
    }
}

File vendor\maatwebsite\excel\src\Mixins\StoreCollection.php is pretty much the same, with only one method storeExcel().

Steps To Reproduce:

N/A

schonhoff commented 3 years ago

Hello,

I have the same problem. I hope that there will be a fix for it.

pokono commented 3 years ago

Any work around? Dives me up the wall..

redalpha01 commented 2 years ago

They uhhh, are mixins that are injected into Laravels collection. This is actually accurate. The only issue is the return.